0% found this document useful (0 votes)
5 views

Strings

Oops through java

Uploaded by

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

Strings

Oops through java

Uploaded by

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

UNIT-I 101

Strings in Java:
A string is a sequence of characters.

Java implements strings as objects of type String class.

When a String object is created, a string is created that cannot be changed.

We can still perform all types of string operations, but each time an existing
string is altered a new String object is created that contains the modifications.

Java provide two classes to create modifiable strings.


They are StringBuffer and StringBuilder.

The classes String, StringBuffer and StringBuilder are defined in java.lang


package. All are final.

UNIT-I 102
Constructors of String class:
String( )
String(char chars[ ])
String(char chars[ ], int startIndex, int numChars)
String(String strObj)
String(byte asciiChars[ ])
String(byte asciiChars[ ],int startIndex,int numChars)
String(StringBuffer strBufObj)
String(int codePoints[ ],int startIndex, int numChars) Added in J2SE 5
String(StringBuilder strBuildObj) Added in J2SE 5

UNIT-I 103
String Literals:

String s = ”abcdefghij”;

String Length:

int length( )

UNIT-I 104
String Concatenation:
The + operator concatenates two strings, producing a String object as the
result.

Example1:
String age = “9”;
String s = “He is “ + age + “ years old. “;
System.out.println( s );
The O/P here is “He is 9 years old.”

Example2:
int age = 9;
String s = “He is “ + age + “ years old. “;
System.out.println( s );

The O/P here is also “He is 9 years old.”

UNIT-I 105
String Concatenation:
Example3:
String s = “abcdefghij “ + 20 + 30;
System.out.println( s );
The O/P here is “abcdefghij 2030”

This is because the compiler automatically converts an operand to its string


equivalent whenever the other operand of the + operator is an instance of
String.

Example4:
String s = “abcdefghij “ + (20 + 30);
System.out.println( s );
The O/P here is “abcdefghij 50”

UNIT-I 106
String Conversion and toString( ):
Java converts data into its string representation during concatenation by
calling one of the overloaded versions of the string conversion method
valueOf( ) defined by String class.
valueOf( ) is overloaded for all simple types and for type Object.
static String valueOf(int num)

static String valueOf(byte num)

static String valueOf(double num)

static String valueOf(long num)

static String valueOf(char chars[ ])

static String valueOf(Object ob)

static String valueOf(char chars[ ],int startIndex, int numChars)


UNIT-I 107
String Conversion and toString( ):
For objects, valueOf( ) calls the toString( ) method on the object.

Every class implements toString( ) because it is defined by Object.

The default implementation of toString( ) is sufficient.

We can override toString( ) and provide our own string representations for
objects.

The syntax is:


String toString( )

UNIT-I 108
Example:

class Rect{ class String1


int length,breadth; {
Rect(int x,int y) { public static void main(String args[])
length=x; {
breadth=y; Rect r1=new Rect(10,20);
} String s1=“Rect:”+r1;
} System.out.println(s1);
class Cube { Cube b1=new Cube(10,20,30);
String s2=“Cube:”+b1;
int length,width,height;
System.out.println(s2);
Cube(int x,int y,int z) {
}
length=x;
}
width=y;
height=z; O/P :
} Rect:Rect@82ba41
public String toString( ) { Cube:Hai I am instance of Cube class
return "Hai I am instance of Cube class";
}
}
UNIT-I 109
Character Extraction:

String object cannot be indexed as if they were a character array.


Many of the String methods employ an index into the string for their operation
beginning at zero.

char charAt(int where)


void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)
byte[ ] getBytes( )
char[ ] toCharArray( )

UNIT-I 110
String Comparision:

boolean equals(String str)


boolean equalsIgnoreCase(String str)

boolean regionMatches(int startIndex,String str2, int str2StartIndex,


int numChars)
boolean regionMatches(boolean ignoreCase, int startIndex,String str2,
int str2StartIndex, int numChars)
boolean startsWith(String str)
boolean endsWith(String str)
int compareTo(String str) <0 if invoking string<str
>0 if invoking string>str
0 if two strings are equal

UNIT-I 111
Searching Strings:
int indexOf(int ch) for first occurrence
int lastIndexOf(int ch) for last occurrence
int indexOf(String str)
int lastIndexOf(String str)
int indexOf(int ch, int startIndex)
int lastIndexOf(int ch, int startIndex)
int indexOf(String str, int startIndex)
int lastIndexOf(String str, int startIndex)

startIndex specifies the index at which point the search begins.

UNIT-I 112
Modifying a String:
String substring(int startIndex)
String substring(int startIndex, int endIndex)

String concat(String str)


String replace(char original, char replacement)
String replace(CharSequence original, CharSequence replacement)
Added in J2SE 5

String trim( )

UNIT-I 113
Changing the Case of Characters within a String:

String toLowerCase( )

String toUpperCase( )

UNIT-I 114
Some other String Methods:
int codePointAt(int i) Added by J2SE 5
int codePointBefore(int i) “
int codePointCount(int start,int end) “
boolean contains(CharSequence str) “
boolean contentEquals(CharSequence str) “
boolean contentEquals(StringBuffer str)
static String format(String fmtstr,Object … args) “
static String format(Locale loc, String fmtstr, Object … args) “
boolean matches(String regExp)
int offsetByCodePoints(int start, int num) “
String replaceFirst(String regExp,String newStr)
String replaceAll(String regExp,String newStr)
String[ ] split(String regExp)
String[ ] split(String regExp, int max)
CharSequence subSequence(int startIndex, int stopIndex)
UNIT-I 115
UNIT-I 116
StringBuffer Constructors:

StringBuffer( ) 16 characters
StringBuffer(int size)
StringBuffer(String str) str+16 characters
StringBuffer(CharSequence chars)

UNIT-I 117
Methods:
int length( ) string length
int capacity( ) total allocated capacity
void ensureCapacity(int capacity) preallocate
void setLength(int len)
char charAt(int where)
void setChatAt(int where, char ch)
void getChars(int sourceStart,int sourceEnd,char target[ ],int
targetStart)

Ensures that the capacity of the buffer is at least equal to the specified minimum.
The new capacity is the larger of:
a. The minimumCapacity argument.
b. Twice the old capacity, plus 2.

UNIT-I 118
Methods:
StringBuffer append(String str)
StringBuffer append(int num)
StringBuffer append(Object obj)

StringBuffer insert(int index, String str)


StringBuffer insert(int index, char ch)
StringBuffer insert(int index, Object obj)

StringBuffer reverse( )

StringBuffer delete(int startIndex, int endIndex)


StringBuffer deleteCharAt(int loc)

StringBuffer replace(int startIndex,int endIndex,String str)

String substring(int startIndex)


String substring(int startIndex, int endIndex)
UNIT-I 119

You might also like