String
String
• The Java String class length() method finds the length of a string.
• The length of the Java string is the same as the Unicode code units of the string.
Signature
• public int length()
Returns
• Length of characters. In other words, the total number of characters present in the
string.
Java String charAt()
• The Java String class charAt() method returns a char value at the given index number.
• The index number starts from 0 and goes to n-1, where n is the length of the string.
• It returns StringIndexOutOfBoundsException, if the given index number is greater
than or equal to this string length or a negative number.
Syntax
• public char charAt(int index)
Java String compareTo()
• The Java String class compareTo() method compares the given string with the
current string. It returns a positive number, negative number, or 0.
• It compares strings based on the Unicode value of each character in the strings.
• It returns a negative number, and if the first string is lexicographically equal to the
second string, it returns 0.
• if s1 > s2, it returns positive number
• if s1 < s2, it returns negative number
• if s1 == s2, it returns 0
Syntax
• public int compareTo(String another String)
Java String concat
• The Java String class concat() method combines specified string at the end of this
string.
• It returns a combined string.
• It is like appending another string.
Signature
• public String concat(String another String)
Java String split()
• The java string split() method splits this string against given regular expression
and returns a char array.
Signature
• There are two signature for split() method in java string.
1. public String split(String regex)
2. public String split(String regex, int limit)
Java String toUpperCase()
• The java string toUpperCase() method returns the string in uppercase letter.
• In other words, it converts all characters of the string into upper case letter.
• public String toUpperCase()
• The Java String class trim() method eliminates leading and trailing spaces.
• public String trim()
Returns
• string with omitted leading and trailing spaces
What is a mutable String?
2. StringBuffer(String str):
• It accepts a string argument that sets the initial contents of the StringBuffer object
and reserves room for 16 more characters without reallocation.
• StringBuffer s = new StringBuffer(“EdsoServices");
Modifier and Type Method Description
Public synchronized delete(int startIndex, int It is used to delete the string from specified
StringBuffer endIndex) startIndex and endIndex.
Public ansyncronise delete(int startIndex, int It is used to delete the string from specified
StringBuilder endIndex) startIndex and endIndex.
• Scanner is a class in java.util package used for obtaining the input of the
primitive types like int, double, etc. and strings.
• To create an object of Scanner class, we usually pass the predefined object
System.in, which represents the standard input stream.
• Scanner in = new Scanner(System.in);
Method Description