Core Java Tutorial

Define Java

C vs C++ vs Java

Java Virtual Machine

Java First Program

Java Data Types

Java Tokens

Java Keywords

Java Operators

Operator Precedence

Java Type Casting

Java Statement

Java If statement

Java Switch statement

Java Loops

Java Jumping Statement

Java Arrays 1D

Java Arrays 2D

Java Variable Size Array

Java Vector

Java Math Methods

Java String Methods

Java String Buffer

User-Defined Method

Java Method Overloading

Java Class & Object

Java Constructor

Java Inheritance

Java Interface

Java Packages

Java Multi-threading

Exceptional Handling

File Handling

Java Applets

Java DataBase Connectivity

Page Stats

Visitor: 221

Java String

String is a collection of characters. In Java, strings are class object. A Java string is not a character array and is not terminated by NULL character.

Syntax: In Java, String is declared as follows:

String s = new String("Java Programming");
	or
String s;
s = new String("Java Programming");
    or
String s = "Java Programming";

Example 1: Input string name and display with hello.

public static void main(String[] args) {
    String s;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter Name: ");
    s = scan.nextLine();
    System.out.println("Hello, " + s);
}

Java String methods

The String class defines a number of methods that allow us to manipulate a string.

Example: toLowerCase()

public static void main(String[] args) {
    String s1;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter string");
    s1 = in.nextLine();
    s1=s1.toLowerCase();
    System.out.println("S1 = " + s1);
}

Example: toUpperCase()

public static void main(String[] args) {
    String s1;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter string");
    s1 = in.nextLine();
    s1=s1.toUpperCase();
    System.out.println("S1 = " + s1);
}

Example: concat()

public static void main(String[] args) {
    String s1, s2, s3;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter 2 string");
    s1 = in.nextLine();
    s2 = in.nextLine();
    s3 = s1 + s2;
    System.out.println("After concat new string is : " + s3);
}

Example: replace()

public static void main(String[] args) {
	String s1;
	Scanner in = new Scanner(System.in);
	System.out.println("Enter string");
	s1 = in.nextLine();
	//s1=s1.replace('a', 'b');
	//s1=s1.replace("java", "cpp");
	s1=s1.replaceFirst("java", "cpp");
	System.out.println("S1 = " + s1);
}

Example: trim()



		

Example: equals()

public static void main(String[] args) {
	String s1, s2;
	Scanner in = new Scanner(System.in);
	System.out.println("Enter 1st string");
	s1 = in.nextLine();
	System.out.println("Enter 2nd string");
	s2 = in.nextLine();
	
	//boolean a = s1.equals(s2);
	boolean a = s1.equalsIgnoreCase(s2);
	if(a)
		System.out.println("Strings are equal");
	else
	   System.out.println("Strings are not equal");
}

Example: compareTo()

public static void main(String[] args) {
	String s1, s2;
	Scanner in = new Scanner(System.in);
	System.out.println("Enter 1st string");
	s1 = in.nextLine();
	System.out.println("Enter 2nd string");
	s2 = in.nextLine();
	
	int i = s1.compareTo(s2);
	//int i = s1.compareToIgnoreCase(s2);
	
	if(i==0)
		System.out.println("Strings are equal");
	else
		if(i<0)
			System.out.println("2nd Strings is greater");
		else
			System.out.println("1st Strings is greater");
}

Example: substring()

public static void main(String[] args) {
	String s1, s2;
	Scanner in = new Scanner(System.in);
	System.out.println("Enter string: ");
	s1 = in.nextLine();
	
	//s2 = s1.substring(3);
	s2 = s1.substring(3,5);
	
	System.out.println("After substring, new string is = " + s2);
}

Example: charAt()

public static void main(String[] args) {
	String s1, s2;
	Scanner in = new Scanner(System.in);
	System.out.println("Enter string");
	s1 = in.nextLine();
	char ch = s1.charAt(5);
	System.out.println("character at 5th position = " + ch);
}

In Java, strings are class objects and implemented using two classes, i.e. String and StringBuffer.