By using java.lang.String.compareTo() method
DemoString.java
[java]
package com.SPlessons;
public class DemoString {
public static void main(String[] args) {
String str1 = "SPlessons", str2 = "Stop thinking start coding";
// comparing str1 and str2
int retval = str1.compareTo(str2);
// prints the return value of the comparison
if (retval < 0) {
System.out.println("str1 is greater than str2");
}
else if (retval == 0) {
System.out.println("str1 is equal to str2");
}
else {
System.out.println("str1 is less than str2");
}
}
}
[/java]
In the above example, given two strings to compare, comparison of two strings completely depends on
Unicode
of every character.
Output: Where string 2 is less than string 1.
[java]str1 is greater than str2[/java]
By using java.lang.String.concat() method
DemoString.java
[java]
package com.SPlessons;
public class DemoString {
public static void main(String[] args) {
// print str1
String str1 = "SPLESSONS-STOP";
System.out.println(str1);
// print str2 concatenated with str1
String str2 = str1.concat(" THINKING");
System.out.println(str2);
// prints str3 concatenated with str2(and str1)
String str3 = str2.concat(" START");
System.out.println(str3);
// prints str3 concatenated with str2(and str1)
String str4 = str3.concat(" CODING");
System.out.println(str4);
}
}
[/java]
In the above example, concatenation has been started from first string to last string.
Output: After the concatenation result is as follows.
[java]
SPLESSONS-STOP
SPLESSONS-STOP THINKING
SPLESSONS-STOP THINKING START
SPLESSONS-STOP THINKING START CODING
[/java]
By using java.lang.String.length() method
DemoString.java
[java]
package com.SPlessons;
public class DemoString {
public static void main(String[] args) {
String str = "SPLESSONS";
// prints length of string
System.out.println("length of string = " + str.length());
}
}
[/java]
In the above example, the placed string is SPLESSONS.
Output: The length of the given string will be represented as follows.
[java]length of string = 9[/java]
By using java.lang.String.startsWith(String prefix) method
DemoString.java
[java]package com.SPlessons;
public class DemoString {
public static void main(String[] args) {
String str = "www.splesson.com";
System.out.println(str);
// the start string to be checked
String startstr1 = "www";
String startstr2 = "http://";
// checks that string starts with given substring
boolean retval1 = str.startsWith(startstr1);
boolean retval2 = str.startsWith(startstr2);
// prints true if the string starts with given substring
System.out.println("starts with " + startstr1 + " ? " + retval1);
System.out.println("starts with " + startstr2 + " ? " + retval2);
}
}
[/java]
The startsWith() method is used to check whether the given string is starting with specified string or not, where a boolean is used to return true or false.
Output: Here the web site is starting from
www.
[java]
www.splesson.com
starts with www ? true
starts with http:// ? false
[/java]
By using Java.lang.String.trim() method
DemoString.java
[java]
package com.SPlessons;
public class DemoString {
public static void main(String[] args) {
// string with leading and trailing white space
String str = " This is SPlessons ";
System.out.print("Before trim = ");
System.out.println(".." + str + "..");
// leading and trailing white space removed
System.out.print("After trim = ");
System.out.println(".." + str.trim() + "..");
}
}
[/java]
The trim() method is used to vanish trailing spaces. In the above example, the given string will be print as first println statement, when trim() performed on the string second println statement will be displayed.
Output: The result will be as follows after performed trim().
[java]
Before trim = .. This is SPlessons ..
After trim = ..This is SPlessons..
[/java]
By using toUpperCase() and toLowerCase() methods
DemoString.java
[java]
package com.SPlessons;
import java.util.Locale;
public class DemoString {
public static void main(String[] args) {
String str1 = "SPlessons";
String str2 = "SPlessons.com";
// using the default system Locale
Locale defloc = Locale.getDefault();
// converts all lower case letters in to upper case letters
System.out.println("string value = " + str1.toUpperCase(defloc));
System.out.println("string value = " + str2.toLowerCase(defloc));
str1 = "stop thinking start coding";
System.out.println("string value 1 = " + str1.toUpperCase(defloc));
str2 = "STOP THINKING START CODING";
System.out.println("string value 2 = " + str2.toLowerCase(defloc));
}
}
[/java]
The
toUpperCase()
method is used to convert all the string characters into capital letters,
toLowerCase()
method is used to convert all the string characters into lower letters.
Output: The result is as follows after performing toUpperCase() method and toLowerCase() methods.
[java]
string value = SPLESSONS
string value = splessons.com
string value 1 = STOP THINKING START CODING
string value 2 = stop thinking start coding[/java]