STRING
String is a predefined class
String is an object
String is an non-primitive data type
String is immutable(unchangeable)
Immutable means unchangeable if using a same
value in another String it doesn't create an
instance memory
To use mutable String we can use StringBuffer
and StringBuilder classes of String
String, StringBuffer and StringBuilder package
in java.lang it's an default package
Mainly String used to store an sequence of
characters(collection of characters) with in
double quotation ""
We can use String in two ways:
literal - using with String data type as
variable
String name="HelloWorld";
non-literal - using with create an object
String name=new String("HelloWorld");
String class have more methods each methods perform
specific operations
String methods are:
length() Returns the length of the string
charAt(int index) Returns the character at a
specific index
substring(int beginIndex, int endIndex)
Returns a substring
contains(String str) Checks if the string
contains a sequence
equals(String another) Compares strings for
exact match
equalsIgnoreCase(String another) Compares
strings ignoring case
toUpperCase() / toLowerCase() Converts case
trim() Removes leading and trailing whitespace
replace(oldChar, newChar) Replaces
characters
split(String regex) Splits the string based on
a pattern
indexOf(String str) Returns the index of a
substring
String methods in program:
package javaprogram;
// String
public class TopicScanner{
public static void main(String[] args) {
//length() Returns the length of the string
String userName="Aravind";
System.out.println("userName is "+userName.length());
//output : userName is 7
//charAt(int index) Returns the character at a
specific index
String character="Aravind";
System.out.println("specific letter from character is
"+character.charAt(3));
//output : specific letter from character is v
//substring(int Index) Returns a substring
String names="AravindGuruswamy";
System.out.println("one letter to from names is
"+names.substring(2));
//output :one letter to from names is
avindGuruswamy
//substring(int beginIndex, int endIndex Returns a
substring
String name="AravindGuruswamy";
System.out.println("one letter to another letter is
"+name.substring(2,6));
//output : one letter to another letter is avin
//contains(String str) Checks if the string
contains a sequence
String find="Aravind";
System.out.println("userName contains is
"+find.contains("v"));
// output : userName is true
//equals(String another) Compares strings for exact
match
String valueone="Aravind";
String valuetwo="Aravind";
System.out.println("userName is
"+valueone.equals(valuetwo));
//output : userName is true
//equalsIgnoreCase(String another) Compares
strings ignoring case
String ignorecaseone="Aravind";
String ignorecasetwo="aravind";
System.out.println("userName is
"+ignorecaseone.equalsIgnoreCase(ignorecasetwo));
//output : userName is true
//toUpperCase() Converts case
String uppercase="aravind";
System.out.println("userName is"+uppercase.toUpperCase());
//output : userName is ARAVIND
//toLowerCase() Converts case
String lowercase="ARAVIND";
System.out.println("userName is"+uppercase.toLowerCase());
//output : userName is aravind
//trim() Removes leading and trailing whitespace
String spaceremove="Ara vind";
System.out.println("userName is "+spaceremove.trim());
//output : userName is Ara vind
//replace(oldChar, newChar) Replaces characters
String replacecharater="Aravind";
System.out.println("userName is
"+replacecharater.replace("A", "B"));
//output : userName is Bravind
//split(String regex)Splits the string based on a
pattern
String splitmethod="Aravind is an developer";
String spacesplit[]=splitmethod.split(" ");
for(String sentence:spacesplit)
{
System.out.println("split each word "+sentence);
}
// output : split each word Aravind
// split each word is
// split each word an
// split each word developer
//indexOf(String str)Returns the index of a
substring
String indexofmethod="Aravind";
int indexofone=indexofmethod.indexOf("Ara");
int indexoftwo=indexofmethod.indexOf("Bra");
System.out.println("userName is "+indexofone);
// output : userName is 0
System.out.println("userName is "+indexoftwo);
// output : userName is -1
//hashcode() Returns the data bytecode adddress
String hashcodemethod="Hellojava";
System.out.println(hashcodemethod.hashCode());
// output : -1093654060
//startsWith(String) Returns true the string start
with specific letter
String startmethod="Hellojava";
System.out.println(startmethod.startsWith("H"));
//output : true
//endsWith(String) Returns true the string end with
specific letter
String endmethod="Hellojava";
System.out.println(endmethod.endsWith("a"));
//output : true
//isEmpty() Returns true if value in String
String value="subhash";
System.out.println(value.isEmpty());
// output : false
//concat() concats two string
String concatvalueone="Ram"; String
concatvaluetwo="Kumar";
System.out.println(concatvalueone.concat(concatvaluetwo));
// RamKumar
//compareTo(string) compares two string value and
returns in positive or negative
String comValue="ABCD"; String comValuetwo="BCDE";
System.out.println(comValue.compareTo(comValuetwo));
// output : -1
}
}
StringBuffer and StringBuilder are predefined class used to mutable
String
StringBuffer and StringBuilder package in java.lang its an default
package
Difference between StringBuffer and StringBuilder
StringBuffer
StringBuffer is predefined class
StringBuffer is mutable (changeable)
StringBuffer is synchronized
StringBuffer is Thread safe we can execute Thread (function) only one
at a time
StringBuffer process are slower than StringBuilder because it’s
synchronized and thread safe
StringBuffer methods are:
append()
insert()
delete()
replace()
reverse()
And etc... We can use String methods in StringBuffer
package javaprogram;
// StringBuffer
public class TopicScanner{
public static void main(String[] args) {
//StringBuffer append method
StringBuffer methodone=new StringBuffer("java");
methodone.append("program");
System.out.println(methodone);
//output: javaprogram
//StringBuffer insert method
StringBuffer methodtwo=new StringBuffer("java");
methodtwo.insert(4,"program");
System.out.println(methodtwo);
//output: javaprogram
//StringBuffer delete method
StringBuffer methodthree=new
StringBuffer("technologies");
methodthree.delete(4,11);
System.out.println(methodthree);
//output: techs
//StringBuffer replace method
StringBuffer methodfour=new
StringBuffer("javaexecute");
methodfour.replace(4,12,"program");
System.out.println(methodfour);
//output: javaprogram
//StringBuffer reverse method
StringBuffer methodfive=new StringBuffer("java");
methodfive.reverse();
System.out.println(methodfive);
//output: avaj
}
}
StringBuilder
StringBuilder is predefined class
StringBuilder is mutable(changeable)
StringBuilder is not-synchronized
StringBuilder process are faster than StringBuffer because it’s not-
synchronized and not-thread safe
StringBuilder is not-thread safe we can execute multiple
threads(functions) at same time
StringBuilder methods are:
append()
insert()
delete()
replace()
reverse()
And etc... We can use String methods in StringBuilder
package javaprogram;
// StringBuilder
public class TopicScanner{
public static void main(String[] args) {
//StringBuilder append method
StringBuffer methodone=new StringBuffer("java");
methodone.append("program");
System.out.println(methodone);
//output: javaprogram
//StringBuilder insert method
StringBuffer methodtwo=new StringBuffer("java");
methodtwo.insert(4,"program");
System.out.println(methodtwo);
//output: javaprogram
//StringBuilder delete method
StringBuffer methodthree=new
StringBuffer("technologies");
methodthree.delete(4,11);
System.out.println(methodthree);
//output: techs
//StringBuilder replace method
StringBuffer methodfour=new
StringBuffer("javaexecute");
methodfour.replace(4,12,"program");
System.out.println(methodfour);
//output: javaprogram
//StringBuilder reverse method
StringBuffer methodfive=new StringBuffer("java");
methodfive.reverse();
System.out.println(methodfive);
//output: avaj
}
}