String is a collection of characters. In Java, string is an class object. A Java string is not a character array and is not terminated by NULL character.
Syntax: In Java, String can be declared as follows:
String s = new String("Java Programming");
String s; s = new String("Java Programming");
String s = "Java Programming";
Example 1: Write a program in java to input name and display it with hello.
import java.util.Scanner; class Hello { public static void main(String[] args) { String s; Scanner scan = new Scanner(System.in); System.out.print("Enter Name: "); s = scan.nextLine(); System.out.println("Hello, " + s); scan.close(); } }
Output:
Enter Name: AnkitThe Java String class defines a number of methods that allow us to manipulate a string.
S.No. | Method | Syntax | Description |
---|---|---|---|
length() | s1.length(); | Returns the length of string 's1' as integer value. | |
toLowerCase() | s2=s1.toLowerCase(); | Convert characters of s1 in lower case and stored in s2. | |
toUpperCase() | s2=s1.toUpperCase(); | Convert characters of s1 in upper case and stored in s2. | |
concat() | s1.concat(s2); | Append contents of s2 in s1. | |
replace() | s2=s1.replace('x', 'y'); | It replaces all 'x' in string s1 with 'y' and store in s2. It replaces all occurrence of characters, only not the string. | |
replaceFirst() | s1=s1.replaceFirst("a", "A"); | Replace only the first occurence of character. | |
trim() | s2=s1.trim(); | It removes all the spaces from the beginning and from the end of the string s1 and stored in s2. | |
equals() | s1.equals(s2); | Compare 2 strings and return true if both the strings are equal otherwise returns false. | |
equalsIgnoreCase() | s1.equalsIgnoreCase(s2); | Compare 2 strings, but it will ignore the cases. | |
compareTo() | i=s1.compareTo(s2); | Compare 2 strings and return integer value Returns 0, if both strings are equal, Returns positive number, if s1>s2, and Returns negative number, if s1<s2. | |
compareToIgnoreCase() | i=s1.compareToIgnoreCase(s2); | Same as compareTo method, except it ignore cases. | |
substring() | s2=s1.substring(0,4); | Return a part from the string. | |
charAt() | ch=s1.charAt(5); | Returns the character at specified position. | |
setCharAt() | s1.setCharAt(n,'x'); | It append character in nth position with a character x. Its return type is void. | |
startsWith() | a.startsWith("hello"); | ||
endsWith() | a.endsWith("ld"); |
In the above table, Data type of s1 and s2 is string, Data type of i and n is int, Data type of ch is character.
import java.util.Scanner; class Length { public static void main(String[] args) { String s; int len; Scanner scan = new Scanner(System.in); System.out.print("Enter String: "); s = scan.nextLine(); len = s.length(); System.out.println("Length = " + len); scan.close(); } }
Output:
Enter String: Java is Funimport java.util.Scanner; class Lower { public static void main(String[] args) { String s1; Scanner sc = new Scanner(System.in); System.out.print("Enter string: "); s1 = sc.nextLine(); s1=s1.toLowerCase(); System.out.println("String in lowercase = " + s1); sc.close(); } }
Output:
Enter string: Java is a Coffeeimport java.util.Scanner; class Upper { public static void main(String[] args) { String s1; Scanner sc = new Scanner(System.in); System.out.print("Enter string: "); s1 = sc.nextLine(); s1=s1.toUpperCase(); System.out.println("String in uppercase = " + s1); sc.close(); } }
Output:
Enter string: Java is a Coffeeimport java.util.Scanner; class Concat { public static void main(String[] args) { String s1, s2, s3; Scanner sc = new Scanner(System.in); System.out.print("Enter first strings: "); s1 = sc.nextLine(); System.out.print("Enter second strings: "); s2 = sc.nextLine(); s3 = s1 + " " + s2; System.out.println("After concat new string is : " + s3); sc.close(); } }
Output:
Enter first strings: java isimport java.util.Scanner; class Replace { public static void main(String[] args) { String s1; Scanner in = new Scanner(System.in); System.out.print("Enter string: "); s1 = in.nextLine(); s1=s1.replace('a', 'A'); System.out.println("After replace string is: " + s1); in.close(); } }
Output:
Enter string: java programmingString s1 = learn oops concepts in cpp; s1=s1.replace("cpp", "java"); // replace cpp with java System.out.println("After replace string is: " + s1);
Output:
After replace string is: learn oops concepts in javaString s1 = java programming; s1=s1.replaceFirst("a", "A"); //only first occurrence is replaced System.out.println("After replace string is: " + s1);
Output:
After replace string is: jAva programmingimport java.util.Scanner; class Trim { public static void main(String[] args) { String s1, s2, s3; Scanner sc = new Scanner(System.in); System.out.print("Enter string: "); s1 = sc.nextLine(); System.out.print("Enter another string: "); s2 = sc.nextLine(); System.out.println("Before trim: " +s1 + s2); s3=s1.trim() + s2.trim(); System.out.print("After trim: " +s3); sc.close(); } }
Output:
Enter string:.......Never.......Stop.......import java.util.Scanner; class Equals { public static void main(String[] args) { String s1, s2; Scanner sc = new Scanner(System.in); System.out.print("Enter 1st string: "); s1 = sc.nextLine(); System.out.print("Enter 2nd string: "); s2 = sc.nextLine(); boolean a = s1.equals(s2); if(a) System.out.println("Strings are equal"); else System.out.println("Strings are not equal"); sc.close(); } }
Output:
Enter 1st string: delhiString s1 = 'delhi'; String s2 = 'DELHI'; boolean a = s1.equalsIgnoreCase(s2); if(a) System.out.println("Strings are equal"); else System.out.println("Strings are not equal");
Output:
Strings are equalimport java.util.Scanner; class Compare { public static void main(String[] args) { String s1, s2; Scanner sc = new Scanner(System.in); System.out.print("Enter 1st string: "); s1 = sc.nextLine(); System.out.print("Enter 2nd string: "); s2 = sc.nextLine(); int i = s1.compareTo(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"); sc.close(); } }
Output:
Enter 1st string: delhiString s1 = 'delhi'; String s2 = 'DELHI'; 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");
Output:
Strings are equalimport java.util.Scanner; class Substring { public static void main(String[] args) { String s1, s2; Scanner sc = new Scanner(System.in); System.out.print("Enter string: "); s1 = sc.nextLine(); s2 = s1.substring(0,4); System.out.println("Substring is = " + s2); sc.close(); } }
Output:
Java is my favourite languageString s1 = 'New Delhi'; String s2 = s1.substring(3); System.out.println("Substring is = " + s2);
Output:
Newimport java.util.Scanner; class CharAt { public static void main(String[] args) { String s1; Scanner sc = new Scanner(System.in); System.out.print("Enter string: "); s1 = sc.nextLine(); char ch = s1.charAt(5); System.out.println("Character at 5th position = " + ch); sc.close(); } }
Output:
Enter string: Programming