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

  • char charAt(int index)
  • int codePointAt(int index)
  • int codePointBefore(int index)
  • int codePointCount(int beginIndex, int endIndex)
  • int compareTo(String anotherString)
  • int compareToIgnoreCase(String str)
  • String concat(String str)
  • boolean contains(CharSequence s)
  • boolean contentEquals(CharSequence cs)
  • boolean contentEquals(StringBuffer sb)
  • static String copyValueOf(char[] data)
  • static String copyValueOf(char[] data, int offset, int count)
  • boolean endsWith(String suffix)
  • boolean equals(Object anObject)
  • boolean equalsIgnoreCase(String anotherString)
  • static String format(Locale l, String format, Object... args)
  • static String format(String format, Object... args)
  • byte[] getBytes()
  • byte[] getBytes(Charset charset)
  • byte[] getBytes(String charsetName)
  • void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
  • int hashCode()
  • int indexOf(int ch)
  • int indexOf(int ch, int fromIndex)
  • int indexOf(String str)
  • int indexOf(String str, int fromIndex)
  • String intern()
  • boolean isEmpty()
  • int lastIndexOf(int ch)
  • int lastIndexOf(int ch, int fromIndex)
  • int lastIndexOf(String str)
  • int lastIndexOf(String str, int fromIndex)
  • int length()
  • boolean matches(String regex)
  • int offsetByCodePoints(int index, int codePointOffset)
  • boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
  • boolean regionMatches(int toffset, String other, int ooffset, int len)
  • String replace(char oldChar, char newChar)
  • String replace(CharSequence target, CharSequence replacement)
  • String replaceAll(String regex, String replacement)
  • String replaceFirst(String regex, String replacement)
  • String[] split(String regex)
  • String[] split(String regex, int limit)
  • boolean startsWith(String prefix)
  • boolean startsWith(String prefix, int toffset)
  • CharSequence subSequence(int beginIndex, int endIndex)
  • String substring(int beginIndex)
  • String substring(int beginIndex, int endIndex)
  • char[] toCharArray()
  • String toLowerCase()
  • String toLowerCase(Locale locale)
  • String toString()
  • String toUpperCase()
  • String toUpperCase(Locale locale)
  • String trim()
  • static String valueOf(boolean b)
  • static String valueOf(char c)
  • static String valueOf(char[] data)
  • static String valueOf(char[] data, int offset, int count)
  • static String valueOf(double d)
  • static String valueOf(float f)
  • static String valueOf(int i)
  • static String valueOf(long l)
  • static String valueOf(Object obj)