1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Split():
import java.util.Arrays;
public class StringDemo
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
String s ="Welcome in Edubridge academy";
String str[]= s.split(" ");
for(int i=0;i<str.length;i++)
{
System.out.println(str[i]);
}
}
22
23
24
25
26
27
s.lastIndexOf(‘s’)
28
Java String compare
1. By equals() method
2. By = = operator
3. By compareTo() method
29
class Teststringcomparison1 The String equals() method compares
{ the original content of the string. It
public static void main(String args[]) compares values of string for equality.
{
String class provides two methods:
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s4));
}
30
}
class Teststringcomparison3
{
public static void main(String args[])
{
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in non pool)
}
} 31
The String compareTo() method compares values
lexicographically and returns an integer value that describes
if first string is less than, equal to or greater than second
class Teststringcomparison4 string.
{
Suppose s1 and s2 are two string variables. If:
public static void main(String args[])
{ s1 == s2 :0
String s1="Sachin"; s1 > s2 :positive value
String s2="Sachin"; s1 < s2 :negative value
String s3="Ratan";
System.out.println(s1.compareTo(s2)); // 0
System.out.println(s1.compareTo(s3)); // 1(because s1>s3)
System.out.println(s3.compareTo(s1)); // -1(because s3 < s1 )
} 32
}
public class StringDemo2
{
public static void main(String[] args)
{
String s="Aman has scored 80 marks";
int digitCount=0;
int alphaCount=0;
for(int i=0;i<s.length();i++)
{
char ch= s.charAt(i);
if(Character.isDigit(ch))
{
digitCount++;
}
if(Character.isAlphabetic(ch))
{
alphaCount++;
}
}
System.out.println("Total number of numeric values "+digitCount);
System.out.println("Total number of numeric values "+alphaCount);
}
33
}
1. StringBuffer is mutuable. (Mutable means one can change the value of the
object.
2. The object created through StringBuffer is stored in the heap.
3. StringBuffer has the same method as the StringBuilder, but each method in
StringBuffer is synchronised that is StringBuffer is thread safe.
4. But being thread safe has disadvantages too as the performance of the
StringBuffer hits due to thread safe property. Thus StringBuilder is faster
than StringBuffer when calling the same method of each class.
5. StringBuffer can be converted to String by using toString() method.
34
StringBuffer s = new StringBuffer(“Hello World”);
// The above object stored in heap and its value can be changed.
s.append(“Java Programming”);
//It modifies the value which allowed in StringBuffer
35
Class StringDemo
HEAP
{
Hello World
public static void main(String args[])
{
StringBuffer s = new StringBuffer(“Hello World”);
s.append(“Java Programmig”);
System.out.println(s); HEAP
} Hello World Java Programming
}
36
class StringManipulation
{
public static void main(String args[])
{
StringBuffer str = new StringBuffer("Object Language");
System.out.println("Original String :"+str);
// Obtaining string Length
System.out.println("Length of string " +str.length());
//Accessing character in string
for(int i=0;i<str.length();i++)
{
int p = i+1;
System.out.println("Characters at position : "+p+" is "+str.charAt(i));
}
37
// inserting a string in the middle
String str2 = new String(str.toString());
int pos = str2.indexOf("Language");
str.insert(pos,"Oriented");
System.out.println("Modified String :"+str);
// Modifying Character
str.setCharAt(6,'-');
System.out.println("String Now : "+str);
// Appending a string at the end
str.append("Improve Security ");
System.out.println("Append String "+str);
} 38
39
String StringBuffer
1. String is Immutable 1. Stringbuffer is mutable
2. String is slow in speed. 2. StringBuffer is fast.
3. Consumes more memory when you 3. Consumes less memory
concat more strings.
4. Override equals() method of object 4. Does not override equals method of object class.
class.
40
41
42
43
StringTokenizer class in Java is used to break a string into tokens.
Example: A StringTokenizer object internally maintains a current
position within the string to be tokenized. ... A token is returned
by taking a substring of the string that was used to create
the StringTokenizerobject.
44
StringTokenizer(String str) : str is string to be tokenized. Considers default delimiters like
new line, space, tab, carriage return and form feed.
StringTokenizer(String str, String delim) : delim is set of delimiters that are used to tokenize
the given string.
StringTokenizer(String str, String delim, boolean flag): The first two parameters have same
meaning. The flag serves following purpose
. If the flag is false, delimiter characters serve to separate tokens. For example, if string is
"hello geeks" and delimiter is " ", then tokens are "hello" and "geeks".
If the flag is true, delimiter characters are considered to be tokens. For example, if string is
"hello geeks" and delimiter is " ", then tokens are "hello", " " and "geeks".
45
import java.util.*;
public class NewClass
{
public static void main(String args[])
{
System.out.println("Using Constructor 1 - ");
StringTokenizer st1 = new StringTokenizer("Hello Geeks How are you", " ");
while (st1.hasMoreTokens())
{
System.out.println(st1.nextToken());
}
System.out.println("Using Constructor 2 - ");
StringTokenizer st2 = new StringTokenizer("JAVA : Code : String", ":“);
while (st2.hasMoreTokens())
{
System.out.println(st2.nextToken());
} System.out.println("Using Constructor 3 - ");
StringTokenizer st3 = new StringTokenizer("JAVA : Code : String", ":", true);
while (st3.hasMoreTokens())
{
System.out.println(st3.nextToken());
}
} 46
47
Java StringJoiner
Java added a new final class StringJoiner in java.util package.
It is used to construct a sequence of characters separated by a delimiter. Now, you can create
string by passing delimiters like comma(,), hyphen(-) etc.
You can also pass prefix and suffix to the char sequence.
48
49
50
import java.util.StringJoiner;
public class StringJoinerExample
{
public static void main(String[] args)
{
// List<Integer> list = new ArrayList<Integer>();
StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter
// Adding values to StringJoiner
joinNames.add("Rahul");
joinNames.add("Raju");
joinNames.add("Peter");
joinNames.add("Raheem");
System.out.println(joinNames);
}
}
51
Java StringJoiner Example: adding prefix and suffix
// importing StringJoiner class
import java.util.StringJoiner;
public class StringJoinerExample {
public static void main(String[] args) {
StringJoiner joinNames = new StringJoiner(",", "[", "]"); // passing comma(,) and square-
brackets as delimiter
// Adding values to StringJoiner
joinNames.add("Rahul");
joinNames.add("Raju");
joinNames.add("Peter");
joinNames.add("Raheem");
System.out.println(joinNames);
}
}
52
StringJoiner Example: Merge Two StringJoiner
The merge() method merges two StringJoiner objects excluding of prefix and suffix of
second StringJoiner object.
53
// importing StringJoiner class
import java.util.StringJoiner;
public class StringJoinerExample {
public static void main(String[] args) {
StringJoiner joinNames = new StringJoiner(",", "[", "]");
// passing comma(,) and square-brackets as delimiter
// Adding values to StringJoiner
joinNames.add("Rahul");
joinNames.add("Raju");
// Creating StringJoiner with :(colon) delimiter
StringJoiner joinNames2 = new StringJoiner(":", “#", “#");
// passing colon(:) and square-brackets as delimiter
// Adding values to StringJoiner
joinNames2.add("Peter");
joinNames2.add("Raheem");
// Merging two StringJoiner
StringJoiner merge = joinNames.merge(joinNames2);
System.out.println(merge); 54
}