Java Tutorial
String str = "input string!";String is also array of characters. String class has multiple constructors to create string object. character array in String constructor.
char[] chars = {'i', 'n', 'p', 'u', 't', ' ', 's', 't', 'r', 'i', 'n', 'g', '!'}; String str = new String(chars);
This page explains some of the functionalities of strings and example programs.
public class JavaString1 { public static void main(String args[]) { String str = "input string!"; char[] chars = {'i', 'n', 'p', 'u', 't', ' ', 's', 't', 'r', 'i', 'n', 'g', '!'}; String str1 = new String(chars); if(str == str1) { System.out.println("Strings are matched!"); } else { System.out.println("Strings are not matched!"); } } }'==' is legal in most programming languages but it's wrong in java for string equal comparison. Output:
$ java JavaString1 Strings are not matched!Correct way to check equal comparison between strings.
public class JavaString1 { public static void main(String args[]) { String str = "input string!"; char[] chars = {'i', 'n', 'p', 'u', 't', ' ', 's', 't', 'r', 'i', 'n', 'g', '!'}; String str1 = new String(chars); if(str.equals(str1)) { System.out.println("Strings are matched!"); } else { System.out.println("Strings are not matched!"); } } }Output:
$ java JavaString1 Strings are matched!
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("String Length: " + str.length()); } }Output:
$ java JavaString2 String Length: 13
string1.concat(string2); or string3 = string1 + string2; string1 += string2;
public class JavaString2 { public static void main(String args[]) { String str1 = "input "; String str2 = "string!"; System.out.println("Concatenated Strings way1: " + str1.concat(str2)); System.out.println("Concatenated Strings way2: " + (str1 + str2)); } }Output:
$ java JavaString2 Concatenated Strings way1: input string! Concatenated Strings way2: input string!
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("Char at index 2: " + str.charAt(2)); } }Output:
$ java JavaString2 Char at index 2: p
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("Is string starts with 'ing!': " + str.startsWith("inp")); } }Output:
$ java JavaString2 Is string starts with 'inp': true
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("Is string ends with 'ing!': " + str.endsWith("ing!")); } }Output:
$ java JavaString2 Is string ends with 'ing!': true
int indexOf(String str) int indexOf(char ch) int indexOf(int ch)
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("index of character 's' in string " + str.indexOf("s")); } }Output:
$ java JavaString2 index of character 's' in string 6
int indexOf(String str, int fromIndex) int indexOf(char ch, int fromIndex) int indexOf(int ch, int fromIndex)if character is not found in string, -1 will be returned.
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("index of character 'n' in string " + str.indexOf("n", 4)); } }Output:
$ java JavaString2 index of character 's' in string 10
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("index of character 88 in string " + str.indexOf(88, 4)); } }Output:
$ java JavaString2 index of character 's' in string -1
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("last index of character 'n' in string " + str.lastIndexOf("n")); } }Output:
$ java JavaString2 last index of character 'n' in string 10Using from last index:
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("last index of character 'n' in string " + str.lastIndexOf("n", 9)); } }Output:
$ java JavaString2 index of character 's' in string 1
public class JavaString2 { public static void main(String args[]) { String str = " input string! "; System.out.println("string after trim: " + str.trim()); } }Output:
$ java JavaString2 string after trim: input string!
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("string after replaced substring: " + str.replace("string", "text")); } }Output:
$ java JavaString2 string after replaced substring: input text!
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("string after replaced substring: " + str.replace('t', 'q')); } }Output:
$ java JavaString2 string after replaced substring: inpuq sqring!
public class JavaString2 { public static void main(String args[]) { String str = "input string!"; System.out.println("string after replaced first occurence substring: " + str.replaceFirst("t", "q")); } }Output:
$ java JavaString2 string after replaced first occurence substring: inpuq string!
public class JavaString2 { public static void main(String args[]) { String str = "Input String!"; System.out.println("To upper case: " + str.toUpperCase()); } }Output:
$ java JavaString2 To upper case: INPUT STRING!
public class JavaString2 { public static void main(String args[]) { String str = "Input String!"; System.out.println("To lower case: " + str.toLowerCase()); } }Output:
$ java JavaString2 To lower case: input string!
public class JavaString2 { public static void main(String args[]) { String str = "Input String!"; System.out.println("Substring starting from index '5': " + str.substring(4)); } }Output:
$ java JavaString2 Substring starting from index '5': t String!
public class JavaString2 { public static void main(String args[]) { String str = "Input String!"; System.out.println("Substring starting from index '5' ending index '7': " + str.substring(4, 7)); } }Output:
$ java JavaString2 Substring starting from index '5' ending index '7': t S
public class JavaString2 { public static void main(String args[]) { String str = "Input String!"; System.out.println("String into char array: " + str.toCharArray()[6]); } }Output:
$ java JavaString2 String into char array: S
public class JavaString2 { public static void main(String args[]) { String str = "Input String!"; String[] arr = str.split(" "); for(int i=0; i<arr.length; i++) { System.out.println("index '" + Integer.toString(i) + "': " + arr[i]); } } }Output:
$ java JavaString2 index '0': Input index '1': String!
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page