Java.lang - SPLessons

Java.lang String Class

Home > Lesson > Chapter 12
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang String Class

Java.lang String Class

shape Description

String is an object in Java programming language, Java itself provided String class to manipulate Strings. The java.lang.String is the class to support more methods on a string, these methods are used to execute operations on a string like comparing, converting, replacing, trimming. The java.lang.String class implements some interfaces such as Serializable, Comparable and CharSequence interfaces. Following is the syntax declaration of java.lang String class. [java] public final class String extends Object implements Serializable, Comparable<String>, CharSequence [/java]

shape Conceptual Figure

The Serializable is like a marker interface means that it does not have data member and method, the purpose of this interface is to mark all java classes then object of these classes will get certain power. The Comparable interface is utilized to sort the user defined class objects, but the developer can sort elements depends on single data member only such as age, id, address. The CharSequence interface underpins read only access to various char sequences.

Methods

shape Methods

Following are the common using methods of string.
Methods Description
compareTo(String) To compare two strings.
concat(String) To concatenate the one string with end of the string.
length() To know length of the String.
startsWith(String) To start the given string with prefix.
trim() To vanish the spaces.
toUpperCase() To change the string characters into uppercase.
toLowerCase() To change the string characters into lowercase.

shape Example

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]

Summary

shape Key Points

  • The length() is used to find the length of the string.
  • The substring() method is used to find the substring of the main string.