-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringDemo.java
More file actions
80 lines (78 loc) · 3.44 KB
/
Copy pathStringDemo.java
File metadata and controls
80 lines (78 loc) · 3.44 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
70
71
72
73
74
75
76
77
78
79
80
import java.util.Scanner;
public class StringDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first string : ");
String first = in.nextLine();
System.out.println();
System.out.print("Enter second string : ");
String second = in.nextLine();
// equals
System.out.println();
if (first.equals(second)){
System.out.println("both are same");
}
//equals ignore case
System.out.println();
if (first.equalsIgnoreCase(second)){
System.out.println("both are equals ignoring case");
}
//==
System.out.println();
if(first==second){
System.out.println("both are ==");
}
//concat
System.out.println();
System.out.println("the concat value is : "+(first.concat(second)));
// for concatenation
System.out.println();
System.out.println("the concatenation value : "+first +" " +second);
// compare to
System.out.println();
System.out.println("first compare to second :"+ (first.compareTo(second)));
System.out.println("second compare to first :"+(second.compareTo(first)));
// to find length
System.out.println();
System.out.println(first+" has : "+first.length()+" characters. ");
System.out.println(second+" has : "+second.length()+" characters. ");
System.out.println(first+" "+ second+" has : "+(first+second).length()+" characters. ");
// replacement
System.out.println();
System.out.println("Value after replacement");
String third = first.replace('n','d');
String fourth = second.replace('o','d');
System.out.println(third);
System.out.println(fourth);
// reverse
System.out.println();
StringBuffer r = new StringBuffer(first);
String fifth = r.reverse().toString();
System.out.println(" the reverse of "+first+" is : "+fifth);
// upper case
System.out.println();
System.out.println(first+" upper case : "+first.toUpperCase());
System.out.println(second+" upper case : "+second.toUpperCase());
System.out.println(third+" upper case : "+third.toUpperCase());
System.out.println(fourth+" upper case : "+fourth.toUpperCase());
System.out.println(fifth+" upper case : "+fifth.toUpperCase());
// lower case
System.out.println();
System.out.println(first+" lower case : "+first.toLowerCase());
System.out.println(second+" lower case : "+second.toLowerCase());
System.out.println(third+" lower case : "+third.toLowerCase());
System.out.println(fourth+" lower case : "+fourth.toLowerCase());
System.out.println(fifth+" lower case : "+fifth.toLowerCase());
// index or finding position
System.out.println();
System.out.println(" Position : "+first.indexOf('a'));
System.out.println(" Position : "+second.indexOf('b'));
System.out.println(" Position : "+third.indexOf('c'));
System.out.println(" Position : "+fourth.indexOf('d'));
System.out.println(" Position : "+fifth.indexOf('e'));
// escape sequencing
System.out.println();
String text = "the text is n * n\\nn\\ ";
System.out.println(text);
}
}