الاثنين، 25 فبراير 2019

String concat() in java with example

String concat() method in java with example:

concat() method is present in the java.lang.String class. concat() method concatenate the given string at a time. This method concatenates the specified string to the end of this string and returns a new string with the new value. This mehtod can be used to concatenate multiple strings one by one. This method can be called multiple times on the string.

Syntax:

public String concat​(String str)


Return value: String


String concat() in java with example


A string that represents the concatenation of this object's characters followed by the string argument's characters.

In Java 8, a new method join() is introduced. String join() method example to concatenate the given string with a delimiter.

الجمعة، 22 فبراير 2019

Java String compareToIgnoreCase​ method example - Internal Implementation

String compareToIgnoreCase​ method in java with example:

compareToIgnoreCase​() method compares two strings lexicographically by comparing the each character in both strings and ignoring case differences. 

This method returns an integer whose sign is that of calling compareTo with normalized versions of the strings where case differences have been eliminated by calling Character.toLowerCase(Character.toUpperCase(character)) on each character. 



String compareToIgnoreCase​()


Syntax:

Here is the syntax of this method.


public int compareToIgnoreCase​(String str)

الخميس، 21 فبراير 2019

String codePointAt() method in java with example - Internal Implementation

String codePointAt() in Java:

The codePointAt(int index) method of String class takes an index as a parameter and returns a character unicode point at that index in String contained by String or we can say charPointAt() method returns the “unicode number” of the character at that index. The index refers to char values (Unicode code units) and the value of index must be lie between 0 to length-1.

If the char value present at the given index lies in the high-surrogate range, the following index is less than the length of this sequence, and the char value at the following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned.


String codePointAt


Syntax:


public int codePointAt​(int index)

الثلاثاء، 19 فبراير 2019

Java 9 String chars() method example - Internal Implementation

Java String chars():


chars method is introduced in package java.lang.String class from Java 9 onwards. This method returns a stream of int zero-extending the char values from this sequence. Any char which maps to a surrogate code point is passed through uninterpreted. If the sequence is mutated while the stream is being read, the result is undefined.


Java String chars() method example



Syntax:

public IntStream chars​()


This method returns IntStream which holds of characters of input string and will have some light performance penalty.

الثلاثاء، 12 فبراير 2019

Java String length method example

Java String length:


String length() is used to get the length of a Java String. The string length method returns the number of characters in String. The length is equal to the number of 16-bit Unicode characters in the string.


Syntax:

The signature of the String length() method is given below.


public int length()


This method returns int value for characters in string.


الاثنين، 11 فبراير 2019

Java 11 String strip(), stripLeading(), stripTrailing() Examples

String 11 strip() method:

Strip has three methods.

1) strip()
2) stripLeading()
3) stripTrailing

In this post, we will learn how to use strip(), stripLeading(), stripTrailing() methods that are introduced in Java 11 version.

public String strip():


strip() method removes all leading and trailing white space and returns a new string.

If this String object represents an empty string, or if all code points in this string are white space, then an empty string is returned. Otherwise, returns a substring of this string beginning with the first code point that is not a white space up to and including the last code point that is not a white space.

public class StringStripExample {
    public static void main(String[] args) {

        String input = "   hello   ";
        System.out.println("Input string length : " + input.length());
        String stripStr = input.strip();
        System.out.println("Stripped string length : " + stripStr.length());
    }
}

الأربعاء، 6 فبراير 2019

Java 8 String API Join() Method Example

1. Java 8 String join() Overview

In this tutorial, We'll learn about Java 8 String API new join() method with example java programs.

join() method is introduced in java.lang.String class to concatenate the given strings with the delimiter.

Join method returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
Join is static method. No need of creating object for String class.

Internally it works based on the new java 8 class StringJoiner class.


Java 8 String API Join() Method Example


Static initializers and blocks in java


In this post, We will write example programs first and then next will see how it is implemented internally.

Java Switch Statement



In this tutorial, We will learn switch statement.
Switch statement is similar to If else statement. If and If-Else statements can be used to support both simple and complex decision logic. In many cases, the switch statement provides a cleaner way to handle complex decision(business) logic.
switch statements depends on the expression "matching" or being "equal" to one of the cases.


We will learn 1) Switch Statement, syntax, example
2) Default case
3) Java 8 Switch statement
4) Break and Fall down/through switch statement

Read more about "If, If Else, nested Condition - Statement in java".