|
| 1 | +# Java Tutorial: String Methods in Java |
| 2 | + |
| 3 | +- String Methods operate on Java Strings. They can be used to find the length of the string, convert to lowercase, etc. |
| 4 | +- Some of the commonly used String methods are: |
| 5 | + |
| 6 | +``` |
| 7 | +String name = “Kishan”; |
| 8 | +``` |
| 9 | +- (Indexes of the above string are as follows: 0-K, 1-i, 2-s, 3-h, 4-a, 5-n) |
| 10 | + |
| 11 | +| **Methods** | **Description** | |
| 12 | +|:--------------|:------------------| |
| 13 | +| **1. length()** | Returns the length of String name. (6 in this case) | |
| 14 | +| **2. toLowerCase()** | Converts all the characters of the string to the lower case letters. | |
| 15 | +| **3. toUpperCase()** | Converts all the characters of the string to the upper case letters. | |
| 16 | +| **4. trim()** | Returns a new String after removing all the leading and trailing spaces from the original string. | |
| 17 | +| **5. substring(int start)** | Returns a substring from start to the end. Substring(3) returns “han”. [Note that indexing starts from 0] | |
| 18 | +| **6. substring(int start, int end)** | Returns a substring from the start index to the end index. The start index is included, and the end is excluded. | |
| 19 | +| **7. replace(‘K’, ‘p’)** | Returns a new string after replacing K with p. pishan is returned in this case. (This method takes char as argument) | |
| 20 | +| **8. startsWith(“Ki”)** | Returns true if the name starts with the string “Ki”. (True in this case) | |
| 21 | +| **9. endsWith(“an”)** | Returns true if the name ends with the string “an”. (True in this case) | |
| 22 | +| **10. charAt(2)** | Returns the character at a given index position. (s in this case) | |
| 23 | +| **11. indexOf(“s”)** | Returns the index of the first occurrence of the specified character in the given string. | |
| 24 | +| **12. lastIndexOf(“h”)** | Returns the last index of the specified character from the given string. (3 in this case) | |
| 25 | +| **13. equals(“Kishan”)** | Returns true if the given string is equal to “Kishan” false otherwise [Case sensitive] | |
| 26 | +| **14.equalsIgnoreCase(“kishan”)** | Returns true if two strings are equal, ignoring the case of characters. | |
| 27 | + |
| 28 | +### Escape Sequence Characters : |
| 29 | +- The sequence of characters after backslash ‘\’ = Escape Sequence Characters |
| 30 | +- Escape Sequence Characters consist of more than one character but represent one character when used within the strings. |
| 31 | +- Examples: \n (newline), \t (tab), \’ (single quote), \\ (backslash), etc. |
| 32 | + |
| 33 | +### Code as described in the video |
| 34 | +``` |
| 35 | +public class cwh_14_string_methods { |
| 36 | + public static void main(String[] args) { |
| 37 | + String name = "Kishan"; |
| 38 | + System.out.println(name); |
| 39 | + |
| 40 | + int value = name.length(); |
| 41 | + System.out.println(value); |
| 42 | +
|
| 43 | + String lstring = name.toLowerCase(); |
| 44 | + System.out.println(lstring); |
| 45 | +
|
| 46 | + String ustring = name.toUpperCase(); |
| 47 | + System.out.println(ustring); |
| 48 | +
|
| 49 | + String nonTrimmedString = " Kishan "; |
| 50 | + System.out.println(nonTrimmedString); |
| 51 | +
|
| 52 | + String trimmedString = nonTrimmedString.trim(); |
| 53 | + System.out.println(trimmedString); |
| 54 | +
|
| 55 | + System.out.println(name.substring(1)); |
| 56 | + System.out.println(name.substring(1,5)); |
| 57 | +
|
| 58 | + System.out.println(name.replace('K', 'p')); |
| 59 | + System.out.println(name.replace("n", "ier")); |
| 60 | +
|
| 61 | + System.out.println(name.startsWith("Kis")); |
| 62 | + System.out.println(name.endsWith("dd")); |
| 63 | +
|
| 64 | + System.out.println(name.charAt(4)); |
| 65 | +
|
| 66 | + String modifiedName = "Harryrryrry"; |
| 67 | + System.out.println(modifiedName.indexOf("rry")); |
| 68 | + System.out.println(modifiedName.indexOf("rry", 4)); |
| 69 | + System.out.println(modifiedName.lastIndexOf("rry", 7)); |
| 70 | +
|
| 71 | + System.out.println(name.equals("Kishan")); |
| 72 | + System.out.println(name.equalsIgnoreCase("KiShAn")); |
| 73 | +
|
| 74 | + System.out.println("I am escape sequence\tdouble quote"); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-14/Ch3Strings.pdf) |
| 80 | + |
| 81 | +### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-14/UltimateJavaCheatSheet.pdf) |
0 commit comments