strings in java
DESCRIPTION
all string functionsTRANSCRIPT
Strings in java
Character Functions
Conversions to Ascii
String functions
Primitive data types
In this chapter
Each character is assigned to ascii
code
Character Functions
Character.isWhiteSpace()
Character.isLetterOrDigit()
Character.isUpperCase()Character.isLetter()Character.isDigit()
Character.isLowerCase()
Character.toLowerCase()Character.toUpperCase()
These functions deal only with character manipulation
Character.isLetter()
Enter a letter :
The entered character is a letter
A
This function checks whether the entered character is a letter or not
It return boolean value True or False
Enter a letter :
The entered character is a digit
3
This function checks whether the entered character is a number or not
It return boolean value True or False
Character.isDigit()
Enter a letter :
The entered character is a letter
A
This function checks whether the entered character is a letter or a digit.
It may be both
It return boolean value True or False
Character.isLetterOrDigit()
Enter a digit : 3
The entered character is a digit
Enter a letter :
There is a white space
( )
This function checks whether There is a blank space
It return boolean value True or False
Character.isWhiteSpace()
Enter a letter :
The entered character is in uppercase
A
This function checks whether the entered character is capital letter or not
It return boolean value True or False
Character.isUpperCase()
Enter a letter :
The entered character is a letter
d
This function checks whether the entered character is a lower case letter or not
It return boolean value True or False
Character.isLowerCase()
Enter a letter :
The character after conversion is a
A
This function converts the capital letter to small letter
Character.toLowerCase()
Enter a letter :
The character after conversion is H
h
This function converts the lower case letters to upper case
Character.toUpperCase()
Program to implement character function
class first{void main(char p){if(Character.isLetter(p)
System.out.println(p+ “ is a letter”);else
System.out.println(p+ “ is not a letter”);}}
class first{void main(char p){if(Character.isDigit(p)
System.out.println(p+ “ is a Digit”);else
System.out.println(p+ “ is not a Digit”);}}
class first{void main(char p){if(Character.isLetterOrDigit(p)
System.out.println(p+ “ is a letter or digit”);else
System.out.println(p+ “ is not a letter or digit”);}}
class first{void main(char p){if(Character.isUpperCase(p)
System.out.println(p+ “ is an UpperCase”);else
System.out.println(p+ “ is not an Upper Case”);}}
class first{void main(char p){if(Character.isLowerCase(p)
System.out.println(p+ “ is a LowerCase”);else
System.out.println(p+ “ is not a lower Case”);}}
class first{void main(char p){char x =Character.toUpperCase(p);System.out.println(“ The character after conversion is ” + x);}}
class first{void main(char p){char x =Character.toLowerCase(p);System.out.println(“ The character after conversion is ” + x);}}
Conversions to
Ascii
•
Computer deals with 256 characters to perform any task
Each character is specified with a numeric code called ASCII code which ranges from 0 to 255
ASCII codes
CHARACTERS ASCII CODES
0-9 48-57
A-Z 65-90
a-z 97-122
Examples how to use ASCII
char p = ‘A’;int n = (int)p;
This will give the output as 65 because the ASCII value of A is 65
Examples how to use ASCII
int x = 91;char c = (char) x;
This will give the output as b
B= 66
66+32 = 98
Hence 98 is b
The difference between ASCII code of an upper case and the corresponding lower case is 32
Procedure to convert from uppercase to lower case
Get the ASCII code of upper case character
Increase ASCII code by 32
Obtain character corresponding to the increased ASCII code
Procedure to convert from lowercase to upper case
Get ASCII code of lower case character
Decrease ASCII code by 32
Obtain character corresponding to the decreased ASCII code
String functions
String Functions
lastIndexOf()
indexOf()
Sudstring()
length()
charAt()
toLowerCase()
toUpperCase()
Replace()
These functions deal with strings
equals()
Concat()
equalsIgnoreCase()
compareTo()
trim()
endswith()
startswith()
length()
This is used to find the length of the given string
Syntax:
int var = string var.length();
Ex
String s = “COMPUTER”;Int l = s.length();System.out.println(l);
The output will be 8
charAt()
This is used to find the character at a particular position of the given string
Syntax:
char var = string var.charAt(index);
Ex
String s = “COMPUTER”;char c = s.charAt(3);System.out.println(c);
The output will be P