-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.java
More file actions
26 lines (20 loc) · 1.05 KB
/
String.java
File metadata and controls
26 lines (20 loc) · 1.05 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
Task one would be to study about the string compare function and try to impliment it
The Java String compareTo() method is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value.
Example:
public class CompareToExample {
public static void main(String args[]) {
String str1 = "String method tutorial";
String str2 = "compareTo method example";
String str3 = "String method tutorial";
int var1 = str1.compareTo( str2 );
System.out.println("str1 & str2 comparison: "+var1);
int var2 = str1.compareTo( str3 );
System.out.println("str1 & str3 comparison: "+var2);
int var3 = str2.compareTo("compareTo method example");
System.out.println("str2 & string argument comparison: "+var3);
}
}
// output for above code :
// str1 & str2 comparison: -16
// str1 & str3 comparison: 0
// str2 & string argument comparison: 0