Introduction
In this article we are going to describe the generally useable class, the String class in Java. Because most data is input as a string so it has its own importantance.
![string1.png]()
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as an instances of this class.
String class
A string is a data type used in programming, such as an integer and floating point unit, but is used to represent text rather than numbers. It is comprised of a set of characters that can also contain spaces and numbers. For example, the word "abhishek " and the phrase "I am abhishek kumar dubey software engineer " are both strings. Even "12345" could be considered a string, if specified correctly. Typically, programmers must enclose strings in quotation marks for the data to be recognized as a string and not a number or variable name.
![string2.gif]()
Method Details (String operation)
- char charAt(int index)- returns the character at the specified location.
- int compareTo(String other)- returns a negative value if the string comes before another in dictionary order, a positive value if the string comes after another in dictionary order,
or 0 if the strings are equal.
- boolean endsWith(String suffix)- returns true if the string ends with suffix.
- boolean equals(Object other)- returns true if the string equals other.
- boolean equalsIgnoreCase(String other)- returns true if the string equals another, except for upper/lowercase distinction.
- int indexOf(String str)-
- int indexOf(String str,int fromIndex)- return the start of the first substring equal to str, starting at index 0 or at fromIndex.
- int lastIndexOf(String str)-
- int lastIndexOf(String str,int fromIndex)- return the start of the last substring equal to str, starting at index 0 or at fromIndex.
- int length()- returns the length of the string.
- String replace(char oldChar,char newChar)- returns a new string that is obtained by replacing all characters oldChar in the string with newChar.
- boolean startsWith(String prefix)-returns true if the string begins with prefix.
- String substring(int beginIndex)
- String substring(int beginIndex,int endIndex)- return a new string consisting of all characters from beginIndex until the end of the string or until endIndex (exclusive).
- String toLowerCase()- returns a new string containing all characters in the original string, with uppercase characters converted to lower case.
- String toUpperCase()- returns a new string containing all characters in the original string, with lowercase characters converted to upper case.
- String trim()- returns a new string by eliminating all leading and trailing spaces in the original string.
Example
public class StringOperation
{
public static void main(String arg[]) {
String s = " I am abhishek kumar dubey ";
String s1 = new String("abhishek");
System.out.println("the value charat(5)=" + s.charAt(5) + "\n");
System.out.println("CompareTo method value=" + s.compareTo(s1) + "\n");
System.out.println("is End with suffix =" + s.endsWith("dubey") + "\n");
System.out.println("check equals=" + s.equals(s1) + "\n");
System.out.println("check equals=" + s.equalsIgnoreCase(s1) + "\n");
System.out.println("Index of dubey=" + s.indexOf("dubey") + "\n");
System.out.println("Last Index of dubey=" + s.lastIndexOf("dubey")
+ "\n");
System.out.println("The length of String s=" + s.length() + "\n");
System.out.println("Replace 'a' by 'A'=" + s.replace('a', 'A') + "\n");
System.out.println("check prefix=" + s.startsWith("I") + "\n");
System.out.println("Sub String1=" + s.substring(10) + "\n");
System.out.println("Sub String2=" + s.substring(10, 15) + "\n");
System.out.println("change String in lower Case=" + s.toLowerCase()
+ "\n");
System.out.println("change String in upper Case=" + s.toUpperCase()
+ "\n");
System.out.println("Remove the space end and last=" + s.trim() + "\n");
}
}
Output
![Cmd output string.jpg]()