Java - StringsStrings which are widely used in Java programming are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings. Creating Strings: The most direct way to create a string is to write:
String greeting ="Hello world!";
Whenever it encounters a string literal in your code, the compiler creates a String object with its value, in this case, "Hello world!'. As with any other object, you can create String objects by using the new keyword and a constructor. The String class has eleven constructors that allow you to provide the initial value of the string using different sources, such as an array of characters:
public class StringDemo{
public static void main(String args[]){ char[] helloArray ={'h','e','l','l','o','.'}; String helloString =new String(helloArray); System.out.println(helloString); } } This would produce the following result:
hello.
Note: The String class necessity to make alot Builder Classes. is immutable, so that once it is of modifications to Strings of created a String object cannot be changed. If there is a characters, then you should use String Buffer & String Builder Classes. String Length: Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object. After the following two lines of code have been executed, len equals 17:
public class StringDemo{
public static void main(String args[]){ String palindrome ="Dot saw I was Tod"; int len = palindrome.length(); System.out.println("String Length is : "+ len ); } } This would produce the following result:
StringLengthis:17
Concatenating Strings: The String class includes a method for concatenating two strings:
string1.concat(string2);
This returns a new string that is string1 with string2 added to it at the end. You can also use the concat() method with string literals, as in:
"My name is ".concat("Zara");
Strings are more commonly concatenated with the + operator, as in:
"Hello,"+" world"+"!"
which results in:
"Hello, world!"
Let us look at the following example:
public class StringDemo{
public static void main(String args[]){ String string1 ="saw I was "; System.out.println("Dot "+ string1 +"Tod"); } } This would produce the following result:
Dot saw I was Tod
Creating Format Strings: You have printf() and format() methods to print output with formatted numbers. The String class has an equivalent class method, format(), that returns a String object rather than a PrintStream object. Using String's static format() method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of:
System.out.printf("The value of the float variable is "+
"%f, while the value of the integer "+ "variable is %d, and the string "+ "is %s", floatVar, intVar, stringVar); you can write:
String fs;
fs =String.format("The value of the float variable is "+ "%f, while the value of the integer "+ "variable is %d, and the string "+ "is %s", floatVar, intVar, stringVar); System.out.println(fs); String Methods: Here is the list of methods supported by String class:
char charAt(int index) Description: This method returns the character located at the String's specified index. The string indexes start from zero. Syntax: Here is the syntax of this method:
public char charAt(int index)
Parameters: Here is the detail of parameters:
public class Test{
public static void main(String args[]){ String s ="Strings are immutable"; char result = s.charAt(8); System.out.println(result); } } This produces the following result:
a
int compareTo(Object o) Description: There are two variants of this method. First method compares this String to another Object and second method compares two strings lexicographically. Syntax: Here is the syntax of this method:
int compareTo(Object o)
or int compareTo(String anotherString) Parameters: Here is the detail of parameters:
public class Test{
public static void main(String args[]){ String str1 ="Strings are immutable"; String str2 ="Strings are immutable"; String str3 ="Integers are not immutable"; int result = str1.compareTo( str2 ); System.out.println(result); result = str2.compareTo( str3 ); System.out.println(result); result = str3.compareTo( str1 ); System.out.println(result); } } This produces the following result:
0
10 -10 int compareTo(String anotherString) Description: There are two variants of this method. First method compares this String to another Object and second method compares two strings lexicographically. Syntax: Here is the syntax of this method:
int compareTo(Object o)
or int compareTo(String anotherString) Parameters: Here is the detail of parameters:
public class Test{
public static void main(String args[]){ String str1 ="Strings are immutable"; String str2 ="Strings are immutable"; String str3 ="Integers are not immutable"; int result = str1.compareTo( str2 ); System.out.println(result); result = str2.compareTo( str3 ); System.out.println(result); result = str3.compareTo( str1 ); System.out.println(result); } } This produces the following result:
0
10 -10 int compareToIgnoreCase(String str) Description: This method compares two strings lexicographically, ignoring case differences. Syntax: Here is the syntax of this method:
int compareToIgnoreCase(String str)
Parameters: Here is the detail of parameters:
public class Test{
public static void main(String args[]){ String str1 ="Strings are immutable"; String str2 ="Strings are immutable"; String str3 ="Integers are not immutable"; int result = str1.compareToIgnoreCase( str2 ); System.out.println(result); result = str2.compareToIgnoreCase( str3 ); System.out.println(result); result = str3.compareToIgnoreCase( str1 ); System.out.println(result); } } This produces the following result:
0
10 -10 String concat(String str) Description: This method appends one String to the end of another. The method returns a String with the value of the String passed in to the method appended to the end of the String used to invoke this method. Syntax: Here is the syntax of this method:
public String concat(String s)
Parameters: Here is the detail of parameters:
public class Test{
public static void main(String args[]){ String s ="Strings are immutable"; s = s.concat(" all the time"); System.out.println(s); } } This produces the following result:
Strings are immutable all the time
boolean contentEquals(StringBuffer sb) Description: This method returns true if and only if this String represents the same sequence of characters as the specified in StringBuffer. Syntax: Here is the syntax of this method:
public boolean contentEquals(StringBuffer sb)
Parameters: Here is the detail of parameters:
public class Test{
public static void main(String args[]){ String str1 ="Not immutable"; String str2 ="Strings are immutable"; StringBuffer str3 =new StringBuffer("Not immutable"); boolean result = str1.contentEquals( str3 ); System.out.println(result); result = str2.contentEquals( str3 ); System.out.println(result); } } This produces the following result:
true
false static String copyValueOf(char[] data) Description: This method has two different forms:
Here is the syntax of this method:
public staticString copyValueOf(char[] data)
or public staticString copyValueOf(char[] data,int offset,int count) Parameters: Here is the detail of parameters:
public class Test{
public static void main(String args[]){ char[]Str1={'h','e','l','l','o',' ','w','o','r','l','d'}; String Str2=""; Str2=Str2.copyValueOf(Str1); System.out.println("Returned String: "+Str2); Str2=Str2.copyValueOf(Str1,2,6); System.out.println("Returned String: "+Str2); } } This produces the following result:
Returned String: hello world
Returned String: llo wo boolean endsWith(String suffix) Description: This method tests if this string ends with the specified suffix. Syntax: Here is the syntax of this method:
public boolean endsWith(String suffix)
Parameters: Here is the detail of parameters:
public class Test{
public static void main(String args[]){ String Str=new String("This is really not immutable!!"); boolean retVal; retVal =Str.endsWith("immutable!!"); System.out.println("Returned Value = "+ retVal ); retVal =Str.endsWith("immu"); System.out.println("Returned Value = "+ retVal ); } } This produces the following result:
Returned Value = true
Returned Value = false boolean equals(Object anObject) Description: This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Syntax: Here is the syntax of this method:
public boolean equals(Object anObject)
Parameters: Here is the detail of parameters:
public class Test{
public static void main(String args[]){ String Str1=new String("This is really not immutable!!"); String Str2=Str1; String Str3=new String("This is really not immutable!!"); boolean retVal; retVal =Str1.equals(Str2); System.out.println("Returned Value = "+ retVal ); retVal =Str1.equals(Str3); System.out.println("Returned Value = "+ retVal ); } } This produces the following result:
Returned Value = true
Returned Value = true boolean equalsIgnoreCase(String anotherString) Description: This method compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case. Syntax: Here is the syntax of this method:
public boolean equalsIgnoreCase(String anotherString)
Parameters: Here is the detail of parameters:
public class Test{
public static void main(String args[]){ String Str1=new String("This is really not immutable!!"); String Str2=Str1; String Str3=new String("This is really not immutable!!"); String Str4=new String("This IS REALLY NOT IMMUTABLE!!"); boolean retVal; retVal =Str1.equals(Str2); System.out.println("Returned Value = "+ retVal ); retVal =Str1.equals(Str3); System.out.println("Returned Value = "+ retVal ); retVal =Str1.equalsIgnoreCase(Str4); System.out.println("Returned Value = "+ retVal ); } } This produces the following result:
Returned Value = true
Returned Value = true Returned Value = true byte getBytes() Description: This method has following two forms:
Here is the syntax of this method:
public byte[] getBytes(String charsetName)
throwsUnsupportedEncodingException or public byte[] getBytes() Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str1=new String("Welcome to Tutorialspoint.com"); try{ byte[]Str2=Str1.getBytes(); System.out.println("Returned Value "+Str2); Str2=Str1.getBytes("UTF-8"); System.out.println("Returned Value "+Str2); Str2=Str1.getBytes("ISO-8859-1"); System.out.println("Returned Value "+Str2); }catch(UnsupportedEncodingException e){ System.out.println("Unsupported character set"); } } } This produces the following result:
Returned Value [B@192d342
Returned Value [B@15ff48b Returned Value [B@1b90b39 byte[] getBytes(String charsetName) Description: This method has following two forms:
Here is the syntax of this method:
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException or public byte[] getBytes() Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str1=new String("Welcome to Tutorialspoint.com"); try{ byte[]Str2=Str1.getBytes(); System.out.println("Returned Value "+Str2); Str2=Str1.getBytes("UTF-8"); System.out.println("Returned Value "+Str2); Str2=Str1.getBytes("ISO-8859-1"); System.out.println("Returned Value "+Str2); }catch(UnsupportedEncodingException e){ System.out.println("Unsupported character set"); } } } This produces the following result:
Returned Value [B@192d342
Returned Value [B@15ff48b Returned Value [B@1b90b39 void getChars(int srcBegin,int srcEnd,char[]dst,int dstBegin) Description: This method copies characters from this string into the destination character array. Syntax: Here is the syntax of this method:
public void getChars(int srcBegin,
int srcEnd, char[] dst, int dstBegin) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str1=new String("Welcome to Tutorialspoint.com"); char[]Str2=newchar[7]; try{ Str1.getChars(2,9,Str2,0); System.out.print("Copied Value = "); System.out.println(Str2); }catch(Exception ex){ System.out.println("Raised exception..."); } } } This produces the following result:
Copied Value = lcome t
int hashCode() Description: This method returns a hash code for this string. The hash code for a String object is computed as:
s[0]*31^(n-1)+ s[1]*31^(n-2)+...+ s[n-1]
Using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.) Syntax: Here is the syntax of this method:
public int hashCode()
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.println("Hashcode for Str :"+Str.hashCode()); } } This produces the following result:
Hashcode for Str :1186874997
int indexOf(int ch) Description: This method has following different variants:
Here is the syntax of this method:
public int indexOf(int ch )
or public int indexOf(int ch,int fromIndex) or int indexOf(String str) or int indexOf(String str,int fromIndex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); StringSubStr1=new String("Tutorials"); StringSubStr2=new String("Sutorials"); System.out.print("Found Index :"); System.out.println(Str.indexOf('o')); System.out.print("Found Index :"); System.out.println(Str.indexOf('o',5)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr1)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr1,15)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr2)); } } This produces the following result:
Found Index :4
Found Index :9 Found Index :11 Found Index :-1 Found Index :-1 int indexOf(int ch, int fromIndex) Description: This method has following different variants:
Here is the syntax of this method:
public int indexOf(int ch )
or public int indexOf(int ch,int fromIndex) or int indexOf(String str) or int indexOf(String str,int fromIndex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); String SubStr1=new String("Tutorials"); String SubStr2=new String("Sutorials"); System.out.print("Found Index :"); System.out.println(Str.indexOf('o')); System.out.print("Found Index :"); System.out.println(Str.indexOf('o',5)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr1)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr1,15)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr2)); } } This produces the following result:
Found Index :4
Found Index :9 Found Index :11 Found Index :-1 Found Index :-1 int indexOf(String str) Description: This method has following different variants:
Here is the syntax of this method:
public int indexOf(int ch )
or public int indexOf(int ch,int fromIndex) or int indexOf(String str) or int indexOf(String str,int fromIndex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); StringSubStr1=new String("Tutorials"); StringSubStr2=new String("Sutorials"); System.out.print("Found Index :"); System.out.println(Str.indexOf('o')); System.out.print("Found Index :"); System.out.println(Str.indexOf('o',5)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr1)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr1,15)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr2)); } } This produces the following result:
Found Index :4
Found Index :9 Found Index :11 Found Index :-1 Found Index :-1 int indexOf(String str, int fromIndex) Description: This method has following different variants:
Here is the syntax of this method:
public int indexOf(int ch )
or public int indexOf(int ch,int fromIndex) or int indexOf(String str) or int indexOf(String str,int fromIndex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); String SubStr1=new String("Tutorials"); String SubStr2=new String("Sutorials"); System.out.print("Found Index :"); System.out.println(Str.indexOf('o')); System.out.print("Found Index :"); System.out.println(Str.indexOf('o',5)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr1)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr1,15)); System.out.print("Found Index :"); System.out.println(Str.indexOf(SubStr2)); } } This produces the following result:
Found Index :4
Found Index :9 Found Index :11 Found Index :-1 Found Index :-1 String intern() Description: This method returns a canonical representation for the string object. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true. Syntax: Here is the syntax of this method:
public String intern()
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str1=new String("Welcome to Tutorialspoint.com"); String Str2=new String("WELCOME TO SUTORIALSPOINT.COM"); System.out.print("Canonical representation:"); System.out.println(Str1.intern()); System.out.print("Canonical representation:"); System.out.println(Str2.intern()); } } This produces the following result:
Canonical representation: Welcome to Tutorialspoint.com
Canonical representation: WELCOME TO SUTORIALSPOINT.COM int lastIndexOf(int ch) Description: This method has the following variants:
Here is the syntax of this method:
int lastIndexOf(int ch)
or public int lastIndexOf(int ch,int fromIndex) or public int lastIndexOf(String str) or public int lastIndexOf(String str,int fromIndex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); String SubStr1=new String("Tutorials"); String SubStr2=new String("Sutorials"); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf('o')); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf('o',5)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr1)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr1,15)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr2)); } } This produces the following result:
Found Last Index :27
Found Last Index :4 Found Last Index :11 Found Last Index :11 Found Last Index :-1 int lastIndexOf(int ch, int fromIndex) Description: This method has the following variants:
Here is the syntax of this method:
int lastIndexOf(int ch)
or public int lastIndexOf(int ch,int fromIndex) or public int lastIndexOf(String str) or public int lastIndexOf(String str,int fromIndex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); String SubStr1=new String("Tutorials"); String SubStr2=new String("Sutorials"); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf('o')); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf('o',5)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr1)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr1,15)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr2)); } } This produces the following result:
Found Last Index :27
Found Last Index :4 Found Last Index :11 Found Last Index :11 Found Last Index :-1 int lastIndexOf(String str) Description: This method has the following variants:
Here is the syntax of this method:
int lastIndexOf(int ch)
or public int lastIndexOf(int ch,int fromIndex) or public int lastIndexOf(String str) or public int lastIndexOf(String str,int fromIndex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); String SubStr1=new String("Tutorials"); String SubStr2=new String("Sutorials"); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf('o')); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf('o',5)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr1)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr1,15)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr2)); } } This produces the following result:
Found Last Index :27
Found Last Index :4 Found Last Index :11 Found Last Index :11 Found Last Index :-1 int lastIndexOf(String str, int fromIndex) Description: This method has the following variants:
Here is the syntax of this method:
int lastIndexOf(int ch)
or public int lastIndexOf(int ch,int fromIndex) or public int lastIndexOf(String str) or public int lastIndexOf(String str,int fromIndex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); String SubStr1=new String("Tutorials"); String SubStr2=new String("Sutorials"); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf('o')); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf('o',5)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr1)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr1,15)); System.out.print("Found Last Index :"); System.out.println(Str.lastIndexOf(SubStr2)); } } This produces the following result:
Found Last Index :27
Found Last Index :4 Found Last Index :11 Found Last Index :11 Found Last Index :-1 int length() Description: This method returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string. Syntax: Here is the syntax of this method:
public int length()
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str1=new String("Welcome to Tutorialspoint.com"); String Str2=new String("Tutorials"); System.out.print("String Length :"); System.out.println(Str1.length()); System.out.print("String Length :"); System.out.println(Str2.length()); } } This produces the following result:
String Length :29
String Length :9 boolean matches(String regex) Description: This method tells whether or not this string matches the given regular expression. An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression Pattern.matches(regex, str). Syntax: Here is the syntax of this method:
public boolean matches(String regex)
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.matches("(.*)Tutorials(.*)")); System.out.print("Return Value :"); System.out.println(Str.matches("Tutorials")); System.out.print("Return Value :"); System.out.println(Str.matches("Welcome(.*)")); } } This produces the following result:
Return Value :true
Return Value :false Return Value :true boolean regionMatches(boolean ignoreCase,inttoffset, String other, int ooffset, int len) Description: This method has two variants which can be used to test if two string regions are equal. Syntax: Here is the syntax of this method:
public boolean regionMatches(int toffset,
String other, int ooffset, int len) or public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str1=new String("Welcome to Tutorialspoint.com"); String Str2=new String("Tutorials"); String Str3=new String("TUTORIALS"); System.out.print("Return Value :"); System.out.println(Str1.regionMatches(11,Str2,0,9)); System.out.print("Return Value :"); System.out.println(Str1.regionMatches(11,Str3,0,9)); System.out.print("Return Value :"); System.out.println(Str1.regionMatches(true,11,Str3,0,9)); } } This produces the following result:
Return Value :true
Return Value :false
Return Value :true
boolean regionMatches(int toffset, String other, int ooffset, int len) Description: This method has two variants which can be used to test if two string regions are equal. Syntax: Here is the syntax of this method:
public boolean regionMatches(int toffset,
String other, int ooffset, int len) or public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str1=new String("Welcome to Tutorialspoint.com"); String Str2=new String("Tutorials"); String Str3=new String("TUTORIALS"); System.out.print("Return Value :"); System.out.println(Str1.regionMatches(11,Str2,0,9)); System.out.print("Return Value :"); System.out.println(Str1.regionMatches(11,Str3,0,9)); System.out.print("Return Value :"); System.out.println(Str1.regionMatches(true,11,Str3,0,9)); } } This produces the following result:
Return Value :true
Return Value :false Return Value :true String replace(char oldChar, char newChar) Description: This method returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. Syntax: Here is the syntax of this method:
public String replace(char oldChar,char newChar)
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.replace('o','T')); System.out.print("Return Value :"); System.out.println(Str.replace('l','D')); } } This produces the following result:
Return Value :WelcTme tT TutTrialspTint.cTm
Return Value :WeDcome to TutoriaDspoint.com String replaceAll(String regex, String replacement ) Description: This method replaces each substring of this string that matches the given regular expression with the given replacement. Syntax: Here is the syntax of this method:
public String replaceAll(String regex,String replacement)
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.replaceAll("(.*)Tutorials(.*)", "AMROOD")); } } This produces the following result:
Return Value :AMROOD
String replaceFirst(String regex, String replacement) Description: This method replaces the first substring of this string that matches the given regular expression with the given replacement. Syntax: Here is the syntax of this method:
public String replaceFirst(String regex,String replacement)
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.replaceFirst("(.*)Tutorials(.*)", "AMROOD")); System.out.print("Return Value :"); System.out.println(Str.replaceFirst("Tutorials","AMROOD")); } } This produces the following result:
Return Value :AMROOD
Return Value :Welcome to AMROODpoint.com String[] split(String regex) Description: This method has two variants and splits this string around matches of the given regular expression. Syntax: Here is the syntax of this method:
public String[] split(String regex,int limit)
or public String[] split(String regex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome-to-Tutorialspoint.com"); System.out.println("Return Value :"); for(String retval:Str.split("-",2)){ System.out.println(retval); } System.out.println(""); System.out.println("Return Value :"); for(String retval:Str.split("-",3)){ System.out.println(retval); } System.out.println(""); System.out.println("Return Value :"); for(String retval:Str.split("-",0)){ System.out.println(retval); } System.out.println(""); System.out.println("Return Value :"); for(String retval:Str.split("-")){ System.out.println(retval); } } } This produces the following result:
Return Value :
Welcome to-Tutorialspoint.com Return Value : Welcome to Tutorialspoint.com Return Value: Welcome to Tutorialspoint.com Return Value : Welcome to Tutorialspoint.com String[] split(String regex, int limit) Description: This method has two variants and splits this string around matches of the given regular expression. Syntax: Here is the syntax of this method:
public String[] split(String regex,int limit)
or public String[] split(String regex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome-to-Tutorialspoint.com"); System.out.println("Return Value :"); for(String retval:Str.split("-",2)){ System.out.println(retval); } System.out.println(""); System.out.println("Return Value :"); for(String retval:Str.split("-",3)){ System.out.println(retval); } System.out.println(""); System.out.println("Return Value :"); for(String retval:Str.split("-",0)){ System.out.println(retval); } System.out.println(""); System.out.println("Return Value :"); for(String retval:Str.split("-")){ System.out.println(retval); } } } This produces the following result:
Return Value :
Welcome to-Tutorialspoint.com Return Value : Welcome to Tutorialspoint.com Return Value: Welcome to Tutorialspoint.com Return Value : Welcome to Tutorialspoint.com boolean startsWith(String prefix) Description: This method has two variants and tests if a string starts with the specified prefix beginning a specified index or by default at the beginning. Syntax: Here is the syntax of this method:
public boolean startsWith(String prefix,int toffset)
or public boolean startsWith(String prefix) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.startsWith("Welcome")); System.out.print("Return Value :"); System.out.println(Str.startsWith("Tutorials")); System.out.print("Return Value :"); System.out.println(Str.startsWith("Tutorials",11)); } } This produces the following result:
Return Value :true
Return Value :false Return Value :true boolean startsWith(String prefix, int toffset) Description: This method has two variants and tests if a string starts with the specified prefix beginning a specified index or by default at the beginning. Syntax: Here is the syntax of this method:
public boolean startsWith(String prefix,int toffset)
or public boolean startsWith(String prefix) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.startsWith("Welcome")); System.out.print("Return Value :"); System.out.println(Str.startsWith("Tutorials")); System.out.print("Return Value :"); System.out.println(Str.startsWith("Tutorials",11)); } } This produces the following result:
Return Value :true
Return Value :false Return Value :true CharSequence subSequence(int beginIndex, int endIndex) Description: This method returns a new character sequence that is a subsequence of this sequence. Syntax: Here is the syntax of this method:
public CharSequence subSequence(int beginIndex,int endIndex)
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.subSequence(0,10)); System.out.print("Return Value :"); System.out.println(Str.subSequence(10,15)); } } This produces the following result:
Return Value :Welcome to
Return Value : Tuto String substring(int beginIndex) Description: This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex - 1 if second argument is given. Syntax: Here is the syntax of this method:
public String substring(int beginIndex)
or public String substring(int beginIndex,int endIndex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.substring(10)); System.out.print("Return Value :"); System.out.println(Str.substring(10,15)); } } This produces the following result:
Return Value : Tutorialspoint.com
Return Value : Tuto String substring(int beginIndex, int endIndex) Description: This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex - 1 if second argument is given. Syntax: Here is the syntax of this method:
public String substring(int beginIndex)
or public String substring(int beginIndex,int endIndex) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.substring(10)); System.out.print("Return Value :"); System.out.println(Str.substring(10,15)); } } This produces the following result:
Return Value : Tutorialspoint.com
Return Value : Tuto char[]toCharArray() Description: This method converts this string to a new character array. Syntax: Here is the syntax of this method:
public char[] toCharArray()
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.toCharArray()); } } This produces the following result:
Return Value :Welcome to Tutorialspoint.com
String toLowerCase() Description: This method has two variants. First variant converts all of the characters in this String to lower case using the rules of the given Locale. This is equivalent to calling toLowerCase(Locale.getDefault()). Second variant takes locale as an argument to be used while converting into lower case. Syntax: Here is the syntax of this method:
public String toLowerCase()
or public String toLowerCase(Locale locale) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.toLowerCase()); } } This produces the following result:
Return Value :welcome to tutorialspoint.com
String toLowerCase(Locale locale) Description: This method has two variants. First variant converts all of the characters in this String to lower case using the rules of the given Locale. This is equivalent to calling toLowerCase(Locale.getDefault()). Second variant takes locale as an argument to be used while converting into lower case. Syntax: Here is the syntax of this method:
public String toLowerCase()
or public String toLowerCase(Locale locale) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.toLowerCase()); } } This produces the following result:
Return Value :welcome to tutorialspoint.com
String toString() Description: This method returns itself a string Syntax: Here is the syntax of this method:
public String toString()
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.toString()); } } This produces the following result:
Return Value :Welcome to Tutorialspoint.com
String toUpperCase() Description: This method has two variants. First variant converts all of the characters in this String to upper case using the rules of the given Locale. This is equivalent to calling toUpperCase(Locale.getDefault()). Second variant takes locale as an argument to be used while converting into upper case. Syntax: Here is the syntax of this method:
public String toUpperCase()
or public String toUpperCase(Locale locale) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.toUpperCase()); } } This produces the following result:
Return Value :WELCOME TO TUTORIALSPOINT.COM
String toUpperCase(Locale locale) Description: This method has two variants. First variant converts all of the characters in this String to upper case using the rules of the given Locale. This is equivalent to calling toUpperCase(Locale.getDefault()). Second variant takes locale as an argument to be used while converting into upper case. Syntax: Here is the syntax of this method:
public String toUpperCase()
or public String toUpperCase(Locale locale) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ String Str=new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.toUpperCase()); } } This produces the following result:
Return Value :WELCOME TO TUTORIALSPOINT.COM
String trim() Description: This method returns a copy of the string, with leading and trailing whitespace omitted. Syntax: Here is the syntax of this method:
publicString trim()
Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ "); String Str=new String(" Welcome to Tutorialspoint.com System.out.print("Return Value :"); System.out.println(Str.trim()); } } This produces the following result:
Return Value :Welcome to Tutorialspoint.com
static String valueOf(primitive data type x) Description: This method has followings variants, which depend on the passed parameters. This method returns the string representation of the passed argument.
Here is the syntax of this method:
static String valueOf(boolean b)
or static String valueOf(char c) or static String valueOf(char[] data) or static String valueOf(char[] data,int offset,int count) or static String valueOf(double d) or static String valueOf(float f) or static String valueOf(int i) or static String valueOf(long l) or static String valueOf(Object obj) Parameters: Here is the detail of parameters:
import java.io.*;
public class Test{ public static void main(String args[]){ double d =102939939.939; boolean b =true; long l =1232874; char[] arr ={'a','b','c','d','e','f','g'}; System.out.println("Return Value : "+String.valueOf(d)); System.out.println("Return Value : "+String.valueOf(b)); System.out.println("Return Value : "+String.valueOf(l)); System.out.println("Return Value : "+String.valueOf(arr)); } } This produces the following result:
Return Value : 1.02939939939E8
Return Value : true Return Value : 1232874 Return Value : abcdefg |