-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsefulStringmethods.java
More file actions
69 lines (47 loc) · 2.17 KB
/
UsefulStringmethods.java
File metadata and controls
69 lines (47 loc) · 2.17 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
66
67
68
69
import java.util.Locale;
public class UsefulStringmethods {
public static void main(String [] args){
String name ="John Muriungi";
//length() method it shows the length of a given string
int length = name.length();
System.out.println("the lenth of the string is"+length);
// charAt() method... it returns a character of a string at a given index
char letter =name.charAt(1);
System.out.println("the last character at index 1 is "+letter);
//indexOf() shows the index of the selected letter
int index= name.indexOf("o");
System.out.println("this shows the index of 'o' "+index);
// lastIndexOf() shows the last index of
int lastIndex=name.lastIndexOf("i");
System.out.println("this shows the last index wher 'i' "+lastIndex);
// toUpperCase()..
name=name.toUpperCase();
System.out.println("the string in upper case "+name);
// toLowerCase()...
name=name.toLowerCase();
System.out.println("the string in lower case "+name);
// trim() gets rid of the white space in the string
String withoutWhiteSpace=name.trim();
System.out.println(withoutWhiteSpace);
// replace() will replace the a character of a string with another character ...
// it takes in two values the first is the charcter to be replaced and the second is the character to replace it
String replace=name.replace("o","a");
System.out.println(replace);
//methods that return a boolean value
// isEmpty();
System.out.println("this shows if the string is empty:"+name.isEmpty());
// .contains() checks if the stringhas a said character
if ( name.contains(" ")){
System.out.println( "Your name contains spaces");
}else{
System.out.println("it does not contain spaces ");
}
// equals() checkds if the two strings are the same
name="password";
if (name.equals("password")){
System.out.println("the name cant be password");
}else{
System.out.println("hello "+name);
}
}
}