java lang String Class

java.lang.String Class

String Class in Java

The Java String class represents character strings. A Java string literal consists of zero or more characters enclosed in double quotes. All Java string literals in Java programs, such as "abc", are implemented as instances of Java String class. Java String class objects are immutable, Java String class objects values cannot be changed after Java String class objects are created.

Java String class includes methods

  • for examining individual characters of the sequence
  • for comparing strings, for searching strings
  • for extracting substrings
  • for creating a copy of a string with all characters translated to uppercase or to lowercase

Case mapping is based on the Unicode Standard version specified by the Character class.

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String.

The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values). Unless otherwise noted, methods for comparing Strings do not take locale into account. The Collator class provides methods for finer-grain, locale-sensitive String comparison.

How to use String Class as StringTokenizer Class in Java ?

Java String class is used like Java StringTokenizer class by using the Java String class split method.

Java StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of Java String class or the java.util.regex package instead.

The following example illustrates how the String.split method can be used to break up a string into its basic tokens at each space :

java.lang.String Class split method Example

/* Java String Class split method Example */
/* Save with file name StringSplitMethodExample.java */

public class StringSplitMethodExample
{
	public static void main(String args[])
	{
		String[] result = "this is a test".split("\\s");

		for (int x=0; x < result.length; x++)
		{
		    System.out.println(result[x]);
		}

	}
}
				    

Following java.lang.String example you can learn how to create java.lang.String and how to use java.lang.String.

java.lang.String Example without Constructor

/* java.lang.String Example without Constructor */
/* Save with file name StringExample1.java */

public class StringExample1
{
	public static void main(String args[])
	{
		//java.lang.String DECLARATION

		String s1;

		//ASSIGN A VALUE to java.lang.String WITHOUT CONSTRUCTOR

		s1 = "Huda Tutorials";

		//java.lang.String OUTPUT

		System.out.println("java.lang.String value : " + s1);
	}
}
				    

java.lang.String Example with Default Constructor

/* java.lang.String Example with Default Constructor */
/* Save with file name StringExample2.java */

public class StringExample2
{
	public static void main(String args[])
	{
		//java.lang.String DECLARATION

		String s1;

		//CREATE INSTANCE for java.lang.String USING DEFAULT CONSTRUCTOR
		//THE VALUE OF STRING IS EMPTY

		s1 = new String();

		//java.lang.String OUTPUT EMPTYSTRING

		System.out.println("String value : " + s1);
	}
}
				    

java.lang.String Example with Parameter Constructor

/* java.lang.String Example with Parameter Constructor */
/* Save with file name StringExample3.java */

public class StringExample3
{
	public static void main(String args[])
	{
		//java.lang.String DECLARATION

		String s1;

		//CREATE INSTANCE of java.lang.String USING PARAMETER CONSTRUCTOR

		s1 = new String("Huda Tutorials");

		//java.lang.String OUTPUT

		System.out.println("String value : " + s1);
	}
}
				    

java.lang.String Example with Concatenation

/* java.lang.String Example with Concatenation */
/* Save with file name StringExample4.java */

public class StringExample4
{
	public static void main(String args[])
	{
		//java.lang.String DECLARATION

		String s1;

		//ASSIGN A VALUE WITH CONCATINATION

		s1 = "Huda" + "Tutorials";

		//java.lang.String OUTPUT

		System.out.println("String value : " + s1);
	}
}
				    

java.lang.String Class Example 5

/* java.lang.String Example */
/* Save with file name StringExample.java */

public class StringExample
{
	public static void main(String args[])
	{
		//java.lang.String DECLARATION

		String s1;

		//CREATE java.lang.String INSTANCE USING PARAMETER CONSTRUCTOR

		s1 = new String("Huda Tutorials");

		//java.lang.String OUTPUT

		System.out.println("String Value : " + s1);

		System.out.println("String Length : " + s1.length());

		System.out.println("charAt 3 : " + s1.charAt(3));

		System.out.println("indexOf a : " + s1.indexOf("a"));

		System.out.println("lastIndexOf a : " + s1.lastIndexOf("a"));

		System.out.println("isEmpty : " + s1.isEmpty());

		System.out.println("startsWith Huda : " + s1.startsWith("Huda"));

		System.out.println("substring : " + s1.substring(5));

		System.out.println("toLowerCase : " + s1.toLowerCase());

		System.out.println("toUpperCase : " + s1.toUpperCase());
	}
}
				    

java.lang.String Comparison Example

/* java.lang.String Comparison Example */
/* Save with file name StringComparison.java */

public class StringComparison
{
	public static void main(String args[])
	{
		//java.lang.String DECLARATION

		String s1, s2;

		//CREATE java.lang.String INSTANCE USING PARAMETER CONSTRUCTOR

		s1 = new String("Huda Tutorials");

		s2 = "huda tutorials";

		//java.lang.String OUTPUT

		System.out.println("equals : " + s1.equals(s2));

		System.out.println("equalsIgnoreCase : " + s1.equalsIgnoreCase(s2));

		System.out.println("compareTo : " + s1.compareTo(s2));

		System.out.println("compareToIgnoreCase : " + s1.compareToIgnoreCase(s2));

		System.out.println("contentEquals : " + s1.contentEquals(s2));
	}
}
				    

java.lang.String String Find

/* java.lang.String String Find Example */
/* Save with file name StringFind.java */

public class StringFind
{
	public static void main(String args[])
	{
		//java.lang.String DECLARATION

		String s1, s2;

		//CREATE java.lang.String INSTANCE USING PARAMETER CONSTRUCTOR

		s1 = new String("Huda Tutorials");

		s2 = "Tutorials";

		//java.lang.String OUTPUT

		System.out.println("contains : " + s1.contains(s2));

		System.out.println("endsWith : " + s1.endsWith(s2));

		System.out.println("indexOf : " + s1.indexOf("a"));

		System.out.println("indexOf from 5th Character : " + s1.indexOf("a",5));

		System.out.println("lastIndexOf : " + s1.lastIndexOf("t"));

		System.out.println("lastIndexOf from 8th Character : " + s1.lastIndexOf("t",8));
	}
}
				    

java.lang.String Manipulation

/* java.lang.String Manipulation Example */
/* Save with file name StringManipulation.java */

public class StringManipulation
{
	public static void main(String args[])
	{
		//java.lang.String DECLARATION

		String s1, s2;

		//CREATE java.lang.String INSTANCE USING PARAMETER CONSTRUCTOR

		s1 = new String("HudaTutorials");

		s2 = ".com";

		//CONCATINATING S1 AND S2 BUT S1 VALUE WON'T CHANGE

		s1.concat(s2);

		System.out.println("concat : " +s1);

		//IF YOU WANT TO CHANGE S1 VALUE YOU SHOULD USE LIKE THIS

		s1 = s1.concat(s2);

		System.out.println("concat : " +s1);

		//REPLACE PART OF A STRING

		System.out.println("I am a Programmer".replace("I am","You are"));

		//PART OF A STRING

		System.out.println("A1234".substring(1));

		//PART OF A STRING

		System.out.println("A1234".substring(1,3));

		//TRIM

		System.out.println("   ABC   ".trim());

		//FORMATTED INT OUTPUT LIKE C

		System.out.println(String.format("%d", 77));

		//FORMATTED INT OUTPUT 5 DIGITS LIKE C

		System.out.println(String.format("%5d", 77));

		//FORMATTED INT OUTPUT 5 DIGITS WITH LEADING ZEROS LIKE C

		System.out.println(String.format("%05d", 77));

		//FORMATTED FLOAT OUTPUT LIKE C

		System.out.println(String.format("%10.2f", 77.55));
	}
}