JAVA String method:
The java.lang.The String class provides many useful methods to perform operations on a sequence of char values.
String-First letter is uppercase, so string is a class.
Method 1:
char charAt(int index)
It returns a char value for the particular index.
Character at given index
Return type: Char
Output comment:
char in =s.charAt(0);
System.out.println(in);
Method 2:
int length()
The String class length() method returns the length of the specified String.
Number of characters in string
Return type: int
Output Comment:
int l=s.length ();
System.out.println(l);
Method 3:
trim()
Removes leading and trailing spaces
Return type: String
Output:
String v= s.trim();
System.out.println(v);
Method 4:
startsWith(prefix)
Checks if the string starts
Return type: boolean
Output:
boolean bo= s.startsWith("Sa");
System.out.println(bo);//True
Method 5:
endsWith(suffix)
Checks if the string ends with a specified sequence.
Return type: boolean
Output:
boolean lo= s.endsWith("Sa");
System.out.println(lo);//false
Method 6:
toCharArray()
Converts to a char array
Return type: char[]
Output:
Method 7:
isEmpty()
Checks if empty
Return type: boolean
Output:
isBlank()
The method is a predefined method in the String class that checks whether a string is empty or contains only whitespace characters.
Method 8:
toUpperCase()
The Java String toUpperCase () method converts it into the String's lowercase letters
Converts to upper case
Return type: String
Output:
String vv =s.toUpperCase();
System. out.println(vv);//SACHIN HELLO
Method 9:
toLowerCase()
The Java String toLowerCase () method converts it into the String's lowercase letters
Converts to lower case
**Return type: **String
Output:
String sv =s.toLowerCase();
System.out.println(sv);//sachin hello
Method 10:
subString method:
Returns substring from index to end
Return type: String (i)
Output:
String sub =s.substring(0)
System.out.println(sub);
Method 11:
replace() method:
Replace character or substring
Output:
String re= s.replace('S', 'H');
System.out.println(re);
Return type: String('i','j');
Method 12:
replaceAll();
Replace all words.
Output:
String str =s.replaceAll("Sachin", "Hello");
System.out.println(str);
Return type: String("String","String");
Method 13:
split();
Splits a string based on a regex.
Output:
Return type:
Method 14:
join();
The method combines multiple strings using a delimiter.
Output:
String result = si.join(" ", "I","Love", "Java");
System.out.println(result);
Return type: String();
Method 15:
contains()
Checks if a sequence of characters is present in the string.
Top comments (0)