Lecture 9 - String, String Buffer, String Tokenizer
Lecture 9 - String, String Buffer, String Tokenizer
CS F213
J. Jennifer Ranjani
email: [email protected]
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.
• String(char[] char_arr)
• String(char[] char_array, int start_index, int count)
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());
sb.append("is ea");
System.out.println(sb.capacity()); Output:
20
StringBuffer sb1 = new StringBuffer(5); 20
sb1.append("Program"); 42
System.out.println(sb1.capacity()); 12
sb1.append(“ming"); 12
System.out.println(sb1.capacity());
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.
Output:
abc,def,ghi
Output:
abc
Output:
o
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));
s1 = "Hello World";
s2 = “World";
System.out.println(s1.contains(s2));