java.lang.String Class

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

In this tutorial you can learn how to create strings and how to use strings.

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

public class StringExample1
{
	public static void main(String args[])
	{
		//DECLARATION
		String s1;
		//ASSIGN A VALUE WITHOUT CONSTRUCTOR
		s1 = "Huda Tutorials";
		//OUTPUT
		System.out.println("String value : " + s1);
	}
}
                        
java.lang.String Class Example 2
/* String Example with Default Constructor */
/* Save with file name StringExample2.java */

public class StringExample2
{
	public static void main(String args[])
	{
		//DECLARATION
		String s1;
		//CREATE INSTANCE USING DEFAULT CONSTRUCTOR
		//THE VALUE OF STRING IS EMPTY
		s1 = new String();
		//OUTPUT EMPTYSTRING
		System.out.println("String value : " + s1);
	}
}
                        
java.lang.String Class Example 3
/* String Example with Parameter Constructor */
/* Save with file name StringExample3.java */

public class StringExample3
{
	public static void main(String args[])
	{
		//DECLARATION
		String s1;
		//CREATE INSTANCE USING PARAMETER CONSTRUCTOR
		s1 = new String("Huda Tutorials");
		//OUTPUT
		System.out.println("String value : " + s1);
	}
}
                        
java.lang.String Class Example 4
/* String Example with Concatenation */
/* Save with file name StringExample4.java */

public class StringExample4
{
	public static void main(String args[])
	{
		//DECLARATION
		String s1;
		//ASSIGN A VALUE WITH CONCATINATION
		s1 = "Huda" + "Tutorials";
		//OUTPUT
		System.out.println("String value : " + s1);
	}
}
                        
java.lang.String Class Example 5
/* String Example */
/* Save with file name StringExample.java */

public class StringExample
{
	public static void main(String args[])
	{
		//DECLARATION
		String s1;
		//CREATE INSTANCE USING PARAMETER CONSTRUCTOR
		s1 = new String("Huda Tutorials");
		//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 Class Example 6
/* String Comparison Example */
/* Save with file name StringComparison.java */

public class StringComparison
{
	public static void main(String args[])
	{
		//DECLARATION
		String s1, s2;
		//CREATE INSTANCE USING PARAMETER CONSTRUCTOR
		s1 = new String("Huda Tutorials");
		s2 = "huda tutorials";
		//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 Class Example 7
/* String Find Example */
/* Save with file name StringFind.java */

public class StringFind
{
	public static void main(String args[])
	{
		//DECLARATION
		String s1, s2;
		//CREATE INSTANCE USING PARAMETER CONSTRUCTOR
		s1 = new String("Huda Tutorials");
		s2 = "Tutorials";
		//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 Class Example 8
/* String Manipulation Example */
/* Save with file name StringManipulation.java */

public class StringManipulation
{
	public static void main(String args[])
	{
		//DECLARATION
		String s1, s2;
		//CREATE 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));
	}
}