Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

string in java

The document provides an overview of the String class in Java, detailing its properties, constructors, and methods such as length(), concatenation, and comparison. It also introduces the StringBuffer class, highlighting its modifiable nature and various methods for string manipulation. Additionally, examples illustrate the use of toString() and other string-related functionalities.

Uploaded by

user23052036
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

string in java

The document provides an overview of the String class in Java, detailing its properties, constructors, and methods such as length(), concatenation, and comparison. It also introduces the StringBuffer class, highlighting its modifiable nature and various methods for string manipulation. Additionally, examples illustrate the use of toString() and other string-related functionalities.

Uploaded by

user23052036
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

String

Prof. Debashis Hati


Email :dhatifcs@kiit.ac.in
Cell No: 9437028209/ 6370961683

07-JAN-2022 Prof. Debashis Hati


String
 It is a predefined class and is available in java.lang.
 final class
 The content of a string can’t be changed.
 Another class is available , StringBuffer whose content can be changed.
Constructors
1. String s1= new String();
Empty string
System.out.println(s1);
2. char x[ ]= {‘a’,’b’};
String s2= new String(x);
System.out.println(s2);
3. String s3= new String(x);
System.out.println(s3);
4. Byte a[ ]= {65,66};
String s4= new String(a);
System.out.println(s4);
String
length() method
It is used to return the no. of characters of the string.

String concatenation
Java does not allow operators with string objects , only exception is +
which concatenates two strings.
int x=10;
System.out.println(x); // 10
String age=“20”;
String y= “ He is “+ age+”years old”;
System.out.println(y);
String z=“ He is “+ x+”years old”
System.out.println(z);
Data conversion with valueOf()
static String valueOf(byte/short/int/long)
static String valueOf(float/double)
static String valueOf(char)
static String valueOf(boolean)
static String valueOf(Object)  valueOf() calls toString( ) methods which
returns the description of the object.

The class Object is the super/base of all predefined and user defined
classes. The Object class has a String toString() method and that
method can be overridden by user defined class.
Use of toString() method
class Money {
int rupee;
int paisa;
Money(){
rupee=0;
paisa=0;
}
Money(int x,int y){
rupee=x;
paisa=y;
}}
class Demo{
public static void main(String t[]){
Money m=new Money(10,50);
System.out.println(m.rupee);
System.out.println(m.paisa);
System.out.println(m);
}}
Use of toString() method
class Money {
int rupee;
int paisa;
Money(){
rupee=0;
paisa=0;
}
Money(int x,int y){
rupee=x;
paisa=y;
}}
class Demo{
public static void main(String t[]){
Money m=new Money(10,50);
System.out.println(m.rupee);
System.out.println(m.paisa);
System.out.println( “Rs”+ m.rupee+”.”+m.paisa+”paisa”);
}} Rs 10.50 paisa
Use of toString() method
class Money {
int rupee;
int paisa;
Money(){
rupee=0;
paisa=0;
}
Money(int x,int y){
rupee=x;
paisa=y;
}}
String toString(){
String z= “Rs”+rupee+”.”+paisa+”paisa”;
retrun z;} }
class Demo{
public static void main(String t[]){
Money m=new Money(10,50);
System.out.println(m);}} Rs 10.50 paisa
Methods of String class
1. Character extraction
int charAt(int index);
char x=“abc”.charAt(1);
System.out.println(x);
char [ ] toCharArray();
2. String comparison
boolean equals(Object );
boolean equalsIgnoreCase(Object );
String s1=“Hello”;
String s2=new String(“Hello”);
String s3=“HELLO”;
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equalsIgnoreCase(s3));
String s4=s1;
Methods of String class
equals() versus ==
System.out.println(s1==s2);
System.out.println(s1==s4);
== OPERATOR
String s1=“hello”;
String s2=“hello”;
System.out.println(s1==s2);

String s1=“hello”;
String s2= new String(“hello”);
System.out.println(s1==s2);

String s1= new String(“hello”);


String s2= new String(“hello”);
System.out.println(s1==s2);
Methods of String class
int compareTo(String str);
Less than zero : invoking string is less than str.
Greater than zero : invoking string is greater than str.
Zero : Two strings are equal.

String str[ ]= { , , ,};


for (i=0; i < str.length ; i++)
for (j=i+1; ij< str.length ; j++)
if (str[i].compareTo(str[j]) < 0
String temp= str[i];
str[i]=str[j];
str[j]= temp;
Methods of String class
3. Searching string
int indexOf(char)  searching the 1st occurrence of a character
int lastIndexOf(char)searching the last occurrence of a
character
String s=“ KIIT”;
System.out.println( s.indexOf(‘I’)); // 1
System.out.println( s.lastIndexOf(‘I’)); //2
4. substring( int startindex); “KIIT”.subString(1);
substring( int startindex,int lastindex); “KIIT”.subString(1,2);
5. concat(String str);
String s1=“one”;
String s2= s1.concat(“two”);
6. replace( char x,char y);
“Hello”.replace(‘l’, ‘w’);
7. trim() removing leading and trailing white spaces
StringBuffer class
 Supports a growable / modifiable string
Constructors
1. StringBuffer() reserves for 16 characters
2. StringBuffer(int size) explictly specifying the size
3. StringBuffer(String s) from a string.

length() and capacity() methods


StringBuffer s1= new StringBuffer();
System.out.println(s1.length()); // 0
System.out.println(s1.capacity()); // 16
StringBuffer s2= new StringBuffer(10);
System.out.println(s2.length()); // 4
System.out.println(s2.capacity()); // 10
StringBuffer s3= new StringBuffer(“kiit);
System.out.println(s3.length()); // 4
System.out.println(s3.capacity() );// 20
StringBuffer class
char charAt(int index);
void setCharAt(int index)

StringBuffer insert(int startindex, String str);


StringBuffer insert(int startindex, char c);
StringBuffer insert(int startindex, Object obj);

StringBuffer reverse()
StringBuffer delete(int start, int end);

You might also like