Monday, 18 November 2019

SE - 15 - String methods in Java !

1) To compare two strings
 - str1.compareTo(str2) [3-way comparison (positive ,negative,0)]
 - str1.equals(str2) [2-way comparison(true,false)]

2) To join 2 strings
 - str1.concat(str2) [Its a method]
 - str1 + str2 [Its a operator]

3) to find the character by index position
 - str1.charAt(1)

4) to compare 2 strings ignoring the case
 - str1.equalsIgnoreCase(str2);

5) to convert a string to upper or lower case
 - Str1.toUppercase();
 - Str1.toLowercase();

6) to remove spaces from both sides of string
 - str1.trim();

7) to trim a part of string
 - Str1.substring(2,12); [based on index]

8) It checks that a string ends\starts with specified suffix and it result boolean result
 - str.endsWith("ABC") [checks if the string ends with ABC]
 - str.startsWith("ABC") [checks if the string starts with ABC]

9) to find the length of a string
- str.length();

10) to replace any word with given word in a string
 - str1.replace("AB","CD")


No comments:

Post a Comment