Interfaces and Classes in Strings in Java
CharBuffer: CharBuffer is utilized to implement the CharSequence interface. With the help of the mentioned class, we can allow character buffers to be utilized instead of CharSequences. We can consider the illustration of these usages is the regular expression package java.util.regex.
String: We can define a string as a series of chars. As we have discussed earlier, in Java, String objects are unchangeable. It is a constant and cannot be modified once it is built.
StringBuffer: This can be defined as a peer class for String that gives the most components. The string represents an immutable fixed-length string, and StringBuffer represents an extensible and writable string.
Syntax:
StringBuffer name = new StringBuffer("StringTutorial");
StringBuilder: Java's StringBuilder represents a variable sequence of characters. Because the Java String class builds an unchangeable string, the StringBuilderclass gives a substitute to the String class for creating mutable strings.
Syntax:
StringBuilder abc = new StringBuilder();
abc.append("NAME");
StringTokenizer: We utilize this class to tokenize a string. The StringTokenizer object internally maintains its current position within the tokenized string. A few steps push on this present position beyond the processed chars. Tokens are returned with the help of the substring of the string utilized to build the StringTokenizer object.
StringJoiner: This can be defined as a class in java.util package that is utilized to create a series of characters (strings) divided by delimiters. Optionally, it starts with the specified prefix and ends with the specified suffix. We can also do it by utilizing the StringBuilder class to add a delimiter after each string, but StringJoiner gives a simple method to do this instead of writing a lot of code.
Syntax:
public StringJoiner(CharSequence delimiter)
Java String Example
public class First{
public static void main(String args[]) {
String name="This"; //defined string by Java string literal
char ch[]={'i','s'};
String name1=new String(ch); //converts char array to string
String name2=new String("Tutorial"); //Java string by new keyword
System.out.println(name);
System.out.println(name1);
System.out.println(name2);
}
}
Output:
This
is
Tutorial
We can analyse the above code and converts a char array into a String object. Then it shows the String objects name, name1, and name2 on the console with the help of the println() method.
Java String class methods
It provides several important methods to help execute operations on a sequence of char values.
No. | Method | Description |
1 | char charAt(int index) | It will result the character value of the certain index. |
2 | int length() | It will result the length of the string. |
3 | static String format(String format, Object_args) | This method gives a formatted string. |
4 | static String format(Locale l, String format, Object_args) | This method will also give a formatted string in the particular locale. |
5 | String substring(int beginIndex) | Results in a substring of the particular starting index. |
6 | String substring(int beginIndex, int endIndex) | This method will result into the particular starting and end index substring. |
7 | boolean contains(CharSequence s) | True or false as the output after comparing with a sequence of character values. |
8 | static String join(CharSequence delimiter, CharSequence... elements) | This method gives the combined string. |
9 | static String join() | This also gives the combined string. |
10 | boolean equals(Object another) | This method will check whether the particular object and the string are equivalent to each other. |
11 | boolean isEmpty() | This method looks in for the null string. |
12 | String concat(String str) | Concatenation of the particular string is performed. |
13 | String replace(char old, char new) | This method will replace every incident of the particular char value. |
14 | String replace(CharSequence old, CharSequence new) | This will also replace every incident of the particular CharSequence. |
15 | static String equalsIgnoreCase(String another) | This method will compare several strings. Uppercase/lowercase is not considered. |
16 | String[] split(String regex) | It will result into a split string that is equivalent to the regular expression. |
17 | String[] split(String regex, int limit) | This method will also result into a split string that is equivalent to the regular expression and the limit. |
18 | String intern() | This method provides the internal string. |
19 | int indexOf(int ch) | It will result the index of the particular char value. |
20 | int indexOf(int ch, int fromIndex) | This method will provide the particular char-valued position starting at the mentioned index. |
21 | int indexOf(String substring) | Results into the particular substring. |
22 | int indexOf(String substring, int fromIndex) | This method will provide the particular substring index starting at the specified index. |
23 | String toLowerCase() | Utilized to get the lowercase string. |
24 | String toLowerCase(Locale l) | It gives the small letters string with the help of the particular locale. |
25 | String toUpperCase() | It will return the input string in capital letters. |
26 | String toUpperCase(Locale l) | It gives the uppercase string with the help of the particular locale. |
27 | String trim() | Remove leading and trailing spaces from this string. |
28 | static String valueOf(int value) | This will convert the particular kind to a string. valueOf() is an overloaded method. |