0% found this document useful (0 votes)
5 views7 pages

String

Uploaded by

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

String

Uploaded by

gamingperfect20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

String manipulation in Java

• char p=‘m’;
• String q=“computer applications”;
Character functions
• Character.isLetter()-to check whether a given argument is an alphabet or not.
Eg: boolean p = Character.isLetter(‘c’); true

• Character.isDigit()- to check whether a given argument is a digit or not.


Eg: boolean p = Character.isLetter(‘7’); true

• Character.isLetterOrDigit()- to check whether a given argument is a letter or a digit or not.


Eg: boolean p = Character.isLetter(‘7’); true
boolean p = Character.isLetter(‘c’); true
boolean p = Character.isLetter(‘*’); false

• Character.isWhiteSpace()- to check for existing blank or gap in a string.


Eg: boolean p = Character.isWhiteSpace(‘ ’); true
boolean p = Character.isWhiteSpace(‘ *’); false

• Character.isUpperCase()- This function will return true if the given argument is an upper case letter or not.
Eg: boolean p = Character.isUpperCase(‘A ’); true
boolean p = Character.isUpperCase(‘ g’); false

• Character.isLowerCase()- This function will return true if the given argument is an lowercase letter or not.
Eg: boolean p = Character.isLowerCase(‘A ’); false
boolean p = Character.isLowerCase(‘ g’); true

• Character.toUpperCase()- This function will return the given argument in upper case .
Eg: boolean p = Character.toUpperCase(‘a ’); A
boolean p = Character.isUpperCase(‘ G’); G
boolean p = Character.isUpperCase(‘ ?’); ?

• Character.toLowerCase()- This function will return the given argument in lower case .
Eg: boolean p = Character.toLowerCase(‘a ’); a
boolean p = Character.isLowerCase(‘ G’); g
boolean p = Character.isLowerCase(‘ ?’); ?
Conversion from characters to ASCII code and vice versa
Characters ASCII Codes

0-9 48 – 57
A-Z 65 – 90
a-z 97 - 122

• Conversion of character to ASCII code


• Syntax:
int variable1 =(int) char variable2;
• Eg: char x= ‘A’;
int n=(int)x; 65
Conversion of ASCII code to character
• Syntax:
int i=98;
char c = (char) i; b
String functions
• length()- to return the length of a string Syntax: int variable = string variable.length();
eg: String str=“COMPUTER”;
int k=str.length(); 8

• charAt() – returns a character from the given index(position) of the string.


Syntax: char variable = string variable.charAt(index);
eg: String str=“COMPUTER”;
char c= str.charAt(3);  ‘P’

• indexOf() –returns the index(position)of the string.


syntax:int variable =stringvariable.indexOf(character);
eg: 1)String str=“COMPUTER”;
int k=str.indexOf(‘P’); 3
2)String s=“MALAYALAM”;
int n=s.indexOf(4,’A’); 5

• last indexOf()- to find the index of last occurrence of a charcter in a string.


eg: String s=“MALAYALAM”;
int n=s.lastindexOf(’A’); 7

• substring() - to extract a part of string.


eg: 1)String str=“COMPUTER”;
String n=str.substring(3); PUTER
String n=str.substring(3,6); PUT
• toLowerCase() - returns the charcters of the string in lowercase.
Eg: String s=“COMPUTER”;
String p=s.toLowerCase();  “computer”

• toUpperCase() – returns a string after converting all the charcters in uppercase letters.
Eg: String s= “computer”;
String p=s.toUpperCase(); “COMPUTER”

• replace() –to replace a character by another character or to replace a string by another


string at all its occurences in the given string.
Eg: 1)String s=“MXLXYXLXM”;
String p=s.replace(‘X’, ‘A’);  “MALAYALAM”
2)String s=“The green bottle is in green bag”;
String p=s.replace(‘green’, ‘red’);

• concat() –to join two strings together.


Eg: String X= “COMPUTER”;
String y= “APPLICATIONS”;
String z= x.concat(y);  “COMPUTERAPPLICATIONS”

• equals() – to comparetwo strings together to check whether they are identical or not.
Eg: 1) String X= “COMPUTER”;
String y= “APPLICATIONS”;
boolean z= x.equals(y);  false
2) Eg: String X= “COMPUTER”;
String y= “computer”;
if( x.equals(y)) System.out.println(“same”) ;
else System.out.println(“different”); different
• equalsIgnoreCase() – used to compare two strings
to ensure whether both are identical or not ignoring
the case.

You might also like