Professional Documents
Culture Documents
Lecture 9 - String, String Buffer, String Tokenizer
Lecture 9 - String, String Buffer, String Tokenizer
CS F213
J. Jennifer Ranjani
email: jennifer.ranjani@pilani.bits-pilani.ac.in
BITS Pilani
Pilani Campus
Array of Objects &
Strings
BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus
Array of Objects
Array of Objects
class Account{
int acc;
String name;
float amount;
void insert(int acc,String name,float amt){
this.acc = acc;
this.name = name;
this.amount = amt;
}
void display(){
System.out.println(acc+" "+name+" "+amount);}
}
for(int i=0;i<3;i++)
{
a[i]= new Account();
a[i].insert(Integer.parseInt(args[3*i]), args[3*i+1], Float.parseFloat(args[3*i+2]));
a[i].display();
}
Output:
}}
111 abc 1000.0
222 bcd 2000.0
333 cde 5000.0
Strings
Unicode
• Problem:
• A particular code value corresponds to different letters
in the various language standards.
• Variable length: Some common characters are
encoded as single bytes, other require two or more
byte.
BITS Pilani, Pilani Campus
String creation
• String(char[] char_arr)
• String(char[] char_array, int start_index, int count)
BITS Pilani, Pilani Campus
Go to menti.com
Use code: 92 31 73 9
String s2 = "JAVA";
System.out.println(s2.equals(s3));
// Output: true
BITS Pilani, Pilani Campus
Example
public class StringExamples
{
public static void main(String[] args)
{
String s1 = "JAVA";
String s2 = "JAVA";
s1 = s1 + "J2EE";
Output : false
System.out.println(s1 == s2);
}
}
s1.concat("J2EE");
String s2=s1.concat("J2EE");
}
}
String Buffer
StringBuffer Constructors
Constructor Description
public StringBuffer() create an empty StringBuffer
create a StringBuffer with initial room
public StringBuffer(int capacity)
for capacity number of characters
create a StringBuffer containing the
public StringBuffer(String str)
characters from str
sb.append(" Programming");
System.out.println(sb.capacity());
Output:
sb.append("is ea");
20
System.out.println(sb.capacity()); 20
42
StringBuffer sb1 = new StringBuffer(5); 12
sb1.append("Program"); 12
System.out.println(sb1.capacity());
sb1.append(“ming");
System.out.println(sb1.capacity());
BITS Pilani, Pilani Campus
ensureCapacity of the
StringBuffer
sb.ensureCapacity(10);
System.out.println(sb.capacity());
Output:
45
sb.ensureCapacity(50);
45
System.out.println(sb.capacity()); 92
String Tokenizer
String Tokenizer
• java.util.StringTokenizer class allows you to break a
string into tokens
• default delimiter set, which is " \t\n\r\f" : the space character, the tab character,
the newline character, the carriage-return character, and the form-feed character.
Constructor Description
StringTokenizer(String str) creates StringTokenizer with
specified string.
StringTokenizer(String str, String creates StringTokenizer with
delim) specified string and delimeter.
StringTokenizer(String str, String creates StringTokenizer with
delim, boolean returnValue) specified string, delimeter and
returnValue. If return value is true,
delimiter characters are considered
to be tokens. If it is false, delimiter
characters serve to separate tokens.
}
BITS Pilani, Pilani Campus
String Tokenizer - Example
import java.util.StringTokenizer;
public class Test{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name/ is \t khan \n");
int i=0,j;
j = st.countTokens();
while (st.hasMoreTokens()) {
System.out.println(st.nextToken("/"));
Output:
i++; my name
} is khan
System.out.println("i: "+i+"and j: "+j);
System.out.println( st.countTokens()); i: 2and j: 4
} 0
}
Output:
abc,def,ghi
Output:
abc
Output:
abc
+
def
:
ghi
• String toLowerCase()
• Converts all characters to lower case
• String toUpperCase()
• Converts all characters to upper case
• String trim()
• Returns the copy of the String, by removing whitespaces at both ends. It does
not affect whitespaces in the middle.
s1 = "Hello World";
s2 = "World";
System.out.println(s1.endsWith(s2));
s1 = "HellO World";
s2 = "hello";
System.out.println(s1.regionMatches(1, s2, 1,
4));
System.out.println(s1.regionMatches(true, 1, s2,
1, 4));
BITS Pilani, Pilani Campus
String Methods
s1 = "Hello World";
s2 = “World";
System.out.println(s1.contains(s2));
BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus