![]() |
![]() |
Java Interface | |
Java Class vs Interface | |
Java Multiple Inheritance | |
Java Nested Interface |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
String is a sequence of characters. It allows us to store multiple characters without using character array. String is immutable means it cannot be modified when assigned
Java string can be implemented using two classes as
String class is already defined in java's java.lang
Package which is used to create strings
The String class object can be created implicitly of explicitly. To create a string implicitly, a String literal is used to create String object automatically. Java Strings are more reliable and predictable.
Syntax
String string_name = "your String"; or String string_name = new String("Your String");
In first Syntax You can directly assign the string while creating an object. In second Syntax you can pass the string to it's constructor while creating an object. Both are valid.
Below example shows how to create and print the String in java.
class StringDemo { String first = "AndroidBerry"; String second = new String(" Easy Learning"); String third = first + second; // it concatenates two strings StringDemo() { System.out.println(first); System.out.println(second); System.out.println(third); } public static void main(String args[]) { StringDemo sd = new StringDemo(); } }
Output
In above example,We have created three strings first,second and third. first string directly assigns the string and second string is assigned in the costructor of String class. third string concatenates first and second string into single string.
String array allows us to store more than one strings into a single String object. Without array we need to create seperate string objects of storing single values.It saves our time and memory space.
String string_name [] = new String [size] = {"string1","string2","stringN"}; or String string_name [] = {"string1","string2","stringN"};
class StringArray { String cities [] = {"USA","Canada","India","Russia","Japan"}; StringArray() { int i; for (i=0;i < cities.length();i++) System.out.println("i = "+cities[i]); } public static void main(String args[]) { StringArray s1 = new StringArray(); } }
There are large number of String methods are available for String manipulation. They saves our time in the program from defining our own methods.
String string_name = "your String"; string_name.methodName();
String s1 = "Android";
String s2 = " Berry" // with empty character space start
int num = 123;
String Method | Example | Used for | Output |
---|---|---|
s1.toLowerCase () | Converts the entire string s1 to lower case | android |
s1.toUpperCase () | Converts the entire string s1 to Upper case | ANDROID |
s2.replace ('B','M') | Replaces all characters with first character to the second character | Merry |
s2.length () | It finds the length of s1 including spaces and returns interger value. | 6 |
s2.trim () | It Removes all white spaces from starting and ending of string. | Berry |
s1.equals (s2) | It compares s1 string with s2. Returns true(1) if they are equal otherwise false(0) | false |
s1.equalsIgnoreCase (s2) | It compares s1 string with s2 by Ignoring capitals and small letters inbetween. Returns true if equal otherwise false. | false |
s1.charAt (3) | It gives the single character from specified index starting from 1 = A in Android. |
d |
s1.compareTo (second) | Returns negative integer if s1 less than s2 and positive when s1 greater than s2. Returns 0 when both are equal | 33 |
s1.compareTo (second) | Returns negative integer by Ignoring cases if s1 less than s2 and positive when s1 greater than s2. Returns 0 when both are equal | 65 |
s1.concat (" Fruit") | It concatenates the given string to s1. New string is always inserted at the end of s1 | Berry Fruit |
s1.substring (2) | It returns the string starting from 2nd index (from 0th location) to the end of string. | droid |
s1.substring (0,3) | It returns the string starting from 0th location and ending with 2nd location (1 as first index) | And |
s1.indexOf ('i') | It returns the location of character 'i' in the given string. (starting from 0) | 5 |
s2.indexOf ('d',3) | It returns the index of d after location 3 (starting from 0) | 6 |
s1.toString () | It creates the String representation of object s1 | Android |
s1.valueOf (num) | It creates String object of variable. Variable can be of type int,char,float,double,long,boolean | 123 |