0% found this document useful (0 votes)
7 views8 pages

String Handling Methods in Java

The document provides an overview of string handling in programming, detailing various methods for manipulating strings such as trimming, converting case, finding lengths, and comparing strings. It includes examples for each method, demonstrating their usage and return types. Key methods discussed include trim(), toLowerCase(), toUpperCase(), length(), charAt(), indexOf(), and several others that facilitate string operations.

Uploaded by

rumasingha112233
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

String Handling Methods in Java

The document provides an overview of string handling in programming, detailing various methods for manipulating strings such as trimming, converting case, finding lengths, and comparing strings. It includes examples for each method, demonstrating their usage and return types. Key methods discussed include trim(), toLowerCase(), toUpperCase(), length(), charAt(), indexOf(), and several others that facilitate string operations.

Uploaded by

rumasingha112233
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

STRING HANDLING

A string is a group of characters enclosed within double quotes. A string is also


referred to as a composite data type because it is composed of character type of
data.
Example - “COMPUTER APPLICATION”
MEMORY REPRESENTATION:
C O M P U T E R A P P L I C A T I O N
INDEX 0 1 2 3 4 5 6 7 8 9 10 11 1 13 14 15 16 17 18 19
2
Letters, numbers, symbols and alphanumeric
Methods for String Handling and their use -
String s = “ COMPUTER “;

1. String trim()
Trims(removes) whitespaces before and after any String.
Return type: - String
[Link]()
Example – String st = [Link](); st= “COMPUTER“
St=COMPUTER

String s = “ COMPUTER “;
String st = [Link]();
[Link](st); “COMPUTER”
[Link](s); “ COMPUTER “
s = [Link]();
[Link](s); “COMPUTER”

2. toLowerCase()
Converts all the characters to lower case.
Return type: - String
Example – String st = [Link]();
st=computer

String st= “AppLIcatiOn24 $”;


String s = [Link]();
s = application24 $
st= [Link]();
st= application

3. toUpperCase()
Converts all the characters to upper case.
Return type: - String

Example – String st = [Link]();


St=COMPUTER

4. length()
Returns the length of the string. Counts the space in the beginning and at the end
of a string.
Return type:- int

Example= int len = [Link]();


Len = 12

5. charAt(int n)
Returns a character from the given index.
Return type:- character

Example- char ch = [Link](4);


ch = M
String d= “JAVA”;
char ch= [Link](3); ch= A

6. indexOf(char ch)
Returns the index value of the first occurrence of the specific character in the
string.
Return type:- int

String s =”application”;

int l = [Link](‘i’); l= 4
int m = [Link](‘i’); m= 8

7. lastIndexOf(char ch)
Returns the index value of the last occurrence of the specific character in the
string.
Return type:- int

Example: String s= “Computer Application”;


Int idx = [Link](‘T’);
idx= 16

8. concat(String st)
Concatenate / joins two strings. Second string is joined with the first string
without any space.
Return type:- String
Example – String s1=”Computer”;
String s2 = “Application”;
String str =[Link](s2);
str=”ComputerApplication”
[Link](s1)
ApplicationComputer
9. boolean equals(String st)
Compares one string with another string. Results in ‘TRUE’ if the characters, the
case of characters and their sequence are the same. Else it will return ‘FALSE’.
Return type:- Boolean
Int a =90 , b=78;
a == b
char ch =’j’ , c = ‘j’;
ch == c
Example – String s1=”COMPUTEr”;
String s2 = “COMPUTER”;
boolean b = [Link](s2);
b = false;

10. boolean equalsIgnoreCase(String st)


Compares one string with another string. Works like method equals but ignores
the case difference.
Return type:- Boolean

Example – String s1=”Computer”;


String s2 = “COMPUTER”;
Boolean b = [Link](s2);
b = true;

11. substring (int beginning_index)


Returns substring of the main string. It begins with the character at a specified
index.
Return type:- String

Example:- String s = “Computer”


String sub = [Link](4);
Sub = uter
String str = “APPLICATION”.substring(3);
Str= LICATION

12. substring (int begin_index, int end_index)


Returns substring from one specific character to next specific character of the
given index value. (excludes character present at the last index)
Return type:- String

Example:- String s = “Computer”


String sub = [Link](2,6);
sub = mput

String x= “BLUEJwithJavA”;
String y= [Link](4,8);
y= Jwit

13. replace(char old, char new)


It replace two characters or two strings.
Return Type:- String

Example- String s= “Application”;


String r = [Link](‘i’,’*’);
r = Appl*cat*on
String str = [Link](‘a’,’*’);
str= Applic*tion

String s=”Red is blue”;


String ns=[Link](“Red”,”Blue”);
ns = Blue is Blue
14. compareTo(String s)
Return type - int
 Compares two strings lexicographically (alphabetical order)
 Method returns:
0, if two string is equal
String w = “class”;
String w2= “ClasS”;
int r = [Link](w2); r =0

Negative number, if one string is less than the other string


Positive number, if one string is greater than the other string

String word1= “Ape”; A=65


String word2= “Zebra”; Z=90

[Link](word2); Output= -25

word1 will come before word2

String s= “social”, t= “social”;


[Link](t);

String word1= “Zebra”; Z=90


String word2= “Ape”; A=65

[Link](word2); Output= 25
word1 would come after word2

String s1 = “break”;
String s2= “brake”;

b = 98 b-b = 0 , r = 114 r-r =0, e-a= 101 - 97= 4

-> Positive value = word 2 comes before word 1


-> Negative value= word 1 comes before word 2

15. compareToIgnoreCase(String s)
Return type- int
Works like method compareTo but ignores the case difference.

16. startsWith(String s)
Checks if the string starts with a specified sequence of characters.
Return type- boolean

Example:- String s= “Computer”;


Boolean b = [Link](‘c’);
b= True

17. endsWith(String s)
Checks if the string ends with a specified sequence of characters.
Return type- boolean

Example:- String s= “Computer”;


Boolean b = [Link](‘r’);
b= False

18. valueOf(all types)


Return type- String
Converts different types of values into String type.

Example int val= 30;


val + 10 = 40
String s = [Link](val);
s= “30”;
s + 25= 3025

float v= 60.8;
String st= [Link](v);
St = 60.8 + 3= 60.83

You might also like