Strings in Java

String is a class of package java.lang which is used to store a set of characters or words.

String class also contains pre-defined methods for String or word operations like copying, merging, finding length of a string, etc.

We don't need to import any class or package to use String, since java.lang classes will be imported by java compiler by default.

Possible usages of Strings

String str="123";

String str="123"+"2";

String a="123"; String s=a+"567";

char a[]={'a','b','c'};

String nstr=new String("123");

System.out.print("Hi"+a);


Important constructors of String class are

String()

It is used to create a new String object with empty value.

Example

class StringTest{

public static void main(String args[]){

String str=new String();

str="123";

System.out.println("The value of variable str is "+str);

}

}

Output

The value of variable str is 123


String(char[] value)

It is used to create a new String object with character array value.

We can use this to convert a character array to String.

Example

class StringTest{

public static void main(String args[]){

char a[]={'a','b','c'};

String str=new String(a);

System.out.println("The value of variable str is "+str);

}

}

Output

The value of variable str is abc


String(char[] value,int offset,int count)

It is used to create a new String object with character array value from the offset position to the number of characters.

Example

class StringTest{

public static void main(String args[]){

char a[]={'a','b','c','d'};

String str=new String(a,2,2);

System.out.println("The value of variable str is "+str);

}

}

Output

The value of variable str is cd


String(String str)

It is used to create a new String object with existing String object value.

Example

class StringTest{

public static void main(String args[]){

char a[]={'a','b','c','d'};

String estr=new String(a);

String str=new String(estr);

System.out.println("The value of variable str is "+str);

}

}

Output

The value of variable str is abcd


String(StringBuilder str)

It is used to create a new String object with existing StringBuilder class object value

We can use this to convert a StringBuilder object to String.

Example

class StringTest{

public static void main(String args[]){

StringBuilder builder=new StringBuilder("123");

String str=new String(builder);

System.out.println("The value of variable str is "+str);

}

}

Output

The value of variable str is 123


String(StringBuffer str)

It is used to create a new String object with existing StringBuffer class object value

We can use this to convert a StringBuffer object to String.

Example

class StringTest{

public static void main(String args[]){

StringBuffer buffer=new StringBuffer("123");

String str=new String(buffer);

System.out.println("The value of variable str is "+str);

}

}

Output

The value of variable str is 123


String(byte[] byte)

It is used to create a new String object with byte array value

We can use this to convert a byte array to String.

class StringTest{

public static void main(String args[]){

byte a[]={'a','b','c'};

String str=new String(a);

System.out.println("The value of variable str is "+str);

}

}

Output

The value of variable str is abc


String(byte[] value,int offset,int count)

It is used to create a new String object with byte array value from the offset position to the number of bytes.

Example

class StringTest{

public static void main(String args[]){

byte a[]={'a','b','c','d'};

String str=new String(a,2,2);

System.out.println("The value of variable str is "+str);

}

}

Output

The value of variable str is cd


Methods of String Class are