-
Notifications
You must be signed in to change notification settings - Fork 857
Expand file tree
/
Copy pathStringMethods.java
More file actions
65 lines (48 loc) · 2.07 KB
/
StringMethods.java
File metadata and controls
65 lines (48 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package strings;
public class StringMethods {
public static void main(String[] args) {
String s = "Hello World";
// String s = new String("Hello World");
// Returns the number of characters in the string.
System.out.println("String length: " + s.length());
// Returns the character at ith index
System.out.println("Character at 3rd position: " + s.charAt(3));
// Return the substring from the ith index character to end of string
System.out.println("Substring: "+s.substring(3));
// Return the substring from i to j-1 index
System.out.println("Substring: "+ s.substring(2, 5));
// Concatenates string2 to end of string1
String s1 = "Hello";
String s2 = " World";
System.out.println("Concatenated String: "+ s1.concat(s2));
// Returns the index within the string of the first occurrence of the specified string
String s4 = "Learn Share Learn";
System.out.println("Index of Share " + s4.indexOf("Share"));
// Returns the index within the string of the first occurrence of the specified string
// Starting at the specified index
System.out.println("Index of a: " + s4.indexOf('a', 3));
// Checking equality of Strings
Boolean out = "Hello".equals("Worlds");
System.out.println("Checking Equality: " + out);
out = "Hello".equals("Hello");
System.out.println("Checking Equality: " + out);
out = "Hello".equalsIgnoreCase("hEllo");
System.out.println("Checing Equality: " + out);
int out1 = s1.compareTo(s2);
System.out.println("If s1 = s2 : " + out);
// Converting cases
String word1 = "Hello World" ;
System.out.println("Changing to lower Case: " + word1.toLowerCase());
// Converting cases
String word2 = "Hello World";
System.out.println("Changing to Upper case: " + word2.toUpperCase());
// Trimming the word
String word3 = " Learn Share Learn ";
System.out.println("Trim the word: "+word3.trim());
// Replacing Characters
String str1 = "Hello ";
System.out.println("Orginal String: "+ str1);
String str2 = str1.replace(" ", " World");
System.out.println("Replaced String: " + str2);
}
}