-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04_Strings
61 lines (41 loc) · 1.67 KB
/
04_Strings
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
import java.util.Scanner;
public class strings {
public static void main(String[] args)
{
String name = "PM ";
System.out.print("Hello I'm ");
System.out.println(name);
int a = 8;
float b = 3.489f;
System.out.printf("%d and %.2f",a, b); // .2 is used for showing only 2 decimal values
System.out.printf("%d and %10f",a, b); // 10 is used for 10 spaces
Scanner sc = new Scanner(System.in);
String st = sc.nextLine();
System.out.println(st);
// STRING METHODS
String word ="Hello";
System.out.println(name);
int value = word.length();
System.out.println(value);
String lstring = word.toLowerCase();
System.out.println(lstring);
String ustring = word.toUpperCase();
System.out.println(ustring);
String nonTrimmedstring =" Hello ";
System.out.println(nonTrimmedstring);
String trimmedString = nonTrimmedstring.trim();
System.out.println(trimmedString);
System.out.println(word.substring(2));
System.out.println(word.substring(1,5));
System.out.println(word.replace('e','a'));
System.out.println(word.startsWith("He"));
System.out.println(word.endsWith("lo"));
System.out.println(word.charAt(4));
System.out.println(word.indexOf("e"));
System.out.println(word.lastIndexOf("e",2));
System.out.println(word.equals("Hello"));
System.out.println(word.equalsIgnoreCase("hello")); // Case of characters is ignored
String modifiedName = "world";
System.out.println(modifiedName);
}
}