Skip to content

Commit 1ccb405

Browse files
committed
feat: Demonstrate String methods endsWith, charAt, startsWith with offset, and character iteration
🧪 Functionality: 1. Check if the string ends with `"Khan"` using `endsWith()`. 2. Fetch character at index `4` using `charAt(4)` — expected output: `'S'`. 3. Check if the substring starting from index 4 starts with `"Shah"` using `startsWith("Shah", 4)`. 4. Print each character of the string using a `for` loop with `charAt(i)`. 🧰 Methods Used: - `String.endsWith(String suffix)` — checks if the string ends with the specified suffix. - `String.charAt(int index)` — returns the character at the given index. - `String.startsWith(String prefix, int offset)` — checks if the string starts with the specified prefix beginning at the given index. - `String.length()` — gets the total number of characters in the string. ✅ Purpose: To demonstrate character-wise access and common string checks (start, end, and offset-based prefix) in Java. Signed-off-by: Somesh diwan <[email protected]>
1 parent aebd2d4 commit 1ccb405

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

Section6StringClassPrinting/src/StringAB3.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ public static void main(String[] args) {
55
System.out.println(str1.endsWith("Khan"));
66

77
System.out.println(str1.charAt(4));
8+
System.out.println(str1.startsWith("Shah", 4));
89

910
//if you want display char of all the strings
10-
for(int i=0; i<str1.length(); i++)
11-
{
11+
for(int i=0; i<str1.length(); i++) {
1212
System.out.print(str1.charAt(i));
1313
}
14-
15-
System.out.println(str1.startsWith("Shah", 4));
1614
}
1715
}

0 commit comments

Comments
 (0)