You are on page 1of 12

Character.

isLetter() :-
This function is used to check whether a given argument is an alphabet or not. It
returns a Boolean type value either true or false.

Syntax: boolean variable = Character.isLetter(character);


boolean p=character.isLetter('c');

The function will return true to the variable p.

boolean p=Character.is Letter('6');

It will return false.

Character.isDigit () :-
This function returns a Boolean type value true if a given argument is a digit
otherwise, false.

Syntax: boolean variable = Character.isDigit(character): e.g.


boolean p=Character is Digit(*7');

It returns true to the variable p.

boolean p=Character.isDigit('G');

It returns false to the variable p.

Character.isLetterOrDigit() :-
This function returns true if the given argument is either a letter or a digit, false
otherwise.

Syntax: boolean variable=Character.isLetterOrDigit(character);

boolean b=Character.isLetterOrDigit('A');

It returns true to the boolean variable b as 'A' is a letter.

boolean b=Character.isLetterOrDigit('r');
It returns true as the given character 'I' is a letter. boolean
b=Character.isLetterOrDigit('9');

It returns true as the given character '9' is a digit.

boolean b=Character.isLetterOrDigit(**');

It returns false as the given character *** is neither a letter nor a digit.

Character.is WhiteSpace() :-
This function can be used to check for existing blank or gap in a String. It will return a
boolean type value (i.e.: true) if the given argument is a white space (blank) and, false
otherwise

Syntax: boolean variable=Character.is WhiteSpace(character);

boolean b=Character is WhiteSpace(' ');

It returns true to the boolean variable b as the given character is a blank.

boolean b=Character.is WhiteSpace(***);

It returns false to the boolean variable b as the given character is not a


blank.

Character.isUpperCase() :-
This function will return true if the given argument is an upper case letter
otherwise false.

Syntax: boolean variable = Character isUpperCase(character);

boolean p=Character.isUpperCase('A');

It returns true to the variable p.

boolean p=Character.isUpperCase('g');

It returns false to the variable p.

Character.isLowerCase() :-
This function will return true if the given argument is a false.
Syntax: boolean variable = Character is LowerCase(character);

boolean p=Character is LowerCase("U");

It returns false to the variable p.

boolean p=Character.is LowerCase('g');

It returns true to the variable p.

Character.toUpperCase() :-
This function returns the given argument in upper case character. The give
remains same if it is already in upper case. It returns the same character if the character
used with the function is non-alphabet.

Syntax: char variable = Character.toUpperCase(character);

char c=Character.toUpperCase('a');

It returns letter A to the variable e.

char c=Character.toUpperCase('G');

It returns G to the variable c.

char c=Character.toUpperCase("?');

It will return the same character because the given character is non-
alphabet.

Character.toLowerCase() :-
This function returns the given argument in lower case character. The given
character remains same if it is already in lower case. It returns the same character if the
character used with the function is non-alphabet.

Syntax: char variable = Character.toLowerCase(character);

char c=Character to LowerCase('A');

It returns letter a to the variable c.

char c=Character to LowerCase('g');


It returns g to the variable c.

char c=Character.toLowerCase(***);

It will return the same character as the given character is non-alphabet.

Syntax: int variablel = (int) char variable2;

char x= 'A';

int n=(int)x;

It results it as 65

Syntax: char variablel = (char) int variable 2:

int i=98 ;

char c = (char) i;

Here, it returns to the variable c.

STRING FUNCTIONS:-

length() :-
This function is used to return the length of a string (i.e., number of characters present
in the string). You must note that the length is an integer number. Hence, it returns an
integer value.

Syntax: int variable = string variable.length();

String str = "COMPUTER":

int k = str.length:

Here, k = 8, as number of characters available in the string str is 8.


charAt() :-
This function returns a character from the given index (position number) of the string

Syntax: char variable = string variable.charAt(index);

String s = "COMPUTER";

char c = s.charAt(3);

Here; c = 'p' as the character at 3rd index is P.

indexOf() :-
This function returns the index (i.e. position number of the string:

Syntax: int variable = string variable.indexOf(character); e.g., String s = "COMPUTER”;

int n = s.indexOf 'P', Here, the value returned by the function is 3. Hence, n =3.

String s = "MALAYALAM”;

int n=s.indexOf (4, 'A'); This function will return the index of 'A' available in the string
after 4 Hence, n = 5.

String s = "COMPUTER”,

int n =s.index of (3, 'C'); It returns NULL as character 'C' is not available after 3rd index.

lastIndexOf() :-
This function is applied to find the index of last occurrence of a character in a String,
Syntax: int variable=String variable.lastIndexOf(character);

String s= "MALAYALAM”; int n=s.lastIndexOf('A'); It returns 7 to the integer variable n.


Hence, n=7.

substring() :-
This function is used to extract a part of string (i.e., simultaneous characters from one
index to another)

Syntax: String variablel = string variable.substring(index); e.g., String s = "COMPUTER”;


String p = s .substring(3); The function will return all the characters from the string
starting from 3rd index Hence, p = PUTER

As the return value of the function is string so, you must use a string variable which
contains the result.

String p = s.substring (3, 6); It returns a part of string from 3rd position to the 6th
position by excluding the character, which is available at 6th position. Hence, p results
in PUT.

toLowerCase() :-
This function returns the characters of the string in lower case letters. If any character is
already in lower case or the character is a special character then it remains same.

Syntax: String variablel = string variable.toLowerCase();

Strings "COMPUTER String pastelowerCase(): Here, p results in computer"

toUpperCase() :-
This function returns a string after converting all the characters in upper case letters. If
any character is already in upper case or the character is a special case character then
it remains same.

Syntax: String variablel = string variable.toUpperCase(); eg. Strings = "computer":

String p=s.toUpperCase(); Here, p results in 'COMPUTER",

replace() :-
This function is used to replace a character by another character or to replace a String
by another String at all its occurrence in the given String,

Syntax: 1. String variablel> = String variable>.replace_character to replace> <character


to

appear 2. String svariablel> = String<variable>.replace<sub string to replace> <sub


string

to appear

String s = "MXLXYXLXM";
String p = s.replace (X', 'A'); Here, each character X of the string is replaced by 'A'.
Hence, the new string p results in "MALAYALAM". 2.

String String s = “The green bottle is in green bag":

TL String p = s.replace ("green", "red"); Here, each sub string "green" of the given string
is replaced by "red": Hence, the new string p results in "The red bottle is in red bag".

concat() :-
This function is applied to concatenate join) two strings together.

Syntax: String variable = string variablel.concat(string variable2):

String x = "COMPUTER" String y = "APPLICATIONS":

String z = x.concat(y); Here, both the strings x and y will be joined together. Hence, z
results in "COMPUTERAPPLICATIONS".

Java language also uses an operator '+' to concatenate two or more strings together.

String x = 'COMPUTER", String y = "APPLICATIONS": String z = "SCIENCE";

String p = x+y; Here, p results in COMPUTERAPPLICATIONS

String p=x+2; Here, p results in COMPUTERSCIENCE.

String p = x +""+y+ + ; Here p results in COMPUTER APLICATIONS SCIENCE

equals() :-
This function is used to compare two strings together to check whether they are identi or
not. It returns a Boolean type value true if both are same, false otherwise.

Syntax; boolean variable = string variablel . equals(string variable2);

String x = 'COMPUTER": String y = "SCIENCE";

boolean z = x.equals(y); Here, z results in false.

String x = "COMPUTER": String y = "computer"> if (X.equals(y))


System.out.println("same");

else
System.out.println("different"): The given program snippet displays a different message.

You must note that equals() function treats corresponding upper case and lower case
characters differently.

equalsignoreCase() :-
This function is also used to compare two strings to ensure whether both are identical or
not after ignoring the case (i.e., corresponding upper and lower case characters are
treated to be same). It also returns a boolean data as true or false. Syntax: boolean
variable = string variablel. equalsignoreCase(string variable2);

String x = "COMPUTER", String y = "computer", boolean p = x.equalsignoreCase(y);


System.out.println(p); The value printed out is true. if(x.equalsignoreCase(y))
System.out.println("same"); else

System.out.println("different"); The given program snippet displays a message same

compareTo() :-
It is also a type of function which compares two strings. It not only checks the equality of
the strings but also checks whether a string is bigger or smaller than other or not

Syntax! int variable = string variablet compare To(string variable2):

Here,

The function returns an integer value depending upon the condition you want to check

String X = "COMPUTER": String y = "SCIENCE": int n = x.compare To(y): n = 0, if x and


y both the strings are same. n > 0, if string x is greater than string y.

n<0, if string x is less than string y. In the above example, the variable n is assigned to a
negative value (ie: n = 16), which is difference between the ASCII value of first
character 'C' of the string "COMPUTER" and 'S' of the string "SCIENCE". It always
compares character by character between the corresponding positions between two
strings.

You must note that the comparisons are based on the order of the corresponding
characters of the strings but not on the basis of the length of strings.

trim() :-
This function is used to remove the blanks from either side of the string. Other blanks,
which are available in between the words, remain unchanged.
Syntax: String variablel = String variable trim(); eg. String s = "COMPUTER
APPLICATIONS":

String p = s.trim();

endswith() :-
This function is used to check whether a given string has specified suffix or not. It
returns a boolean type value true or false accordingly. Syntax: boolean b= string
variablel.endswith(string variable2);

String p= "COMPUTER IS FUN", String b= "FUN": boolean x= p.endswith(b): It will


return x as true.

startwith() :-
This function returns a boolean type value true

if a given string is used as prefix to another

string, false otherwise.

Syntax: boolean b= string variablel.

starts with(string variable2);

e.g. String pa "COMPUTER IS FUN":

String b= "YOUR":

Boolean X- p.starts withchy It will refum xas false al string p does not start with

CONVERSION FROM STRING TO PRIMITIVE TYPES

String to Integer type data :-


Syntax: (a) integer variable = Integer.parseInt(String);

(b) integer variable = Integer valueOf(String),

e.g:- int n; Strings "24":


n=Integer.parseInt(s); or

n=Integer, valueOf(s);

Thus, the variable n results in 24 (without quotes),

String to Long type data :-


Syntax: (a) long variable=Long.parseLong(String);

(b) long variable=Long.valueOf(String);

e.g:- long n; String s= "2465783";

n=Long.parseLong(s); or,

n=Long.valueOf(s);

Thus, the variable n results in 2465783 (without quotes).

String to Float type data :-


Syntax: (a) float variable Float parseFloat(String);

(b) float variable=Float valueOf(String);

e.g:- float n; String s= "24.35";

n=Float parseFloat(s); or

n=Float valueOf(s);

Thus, the variable n results in 24.35 (without quotes)

String to Double type data :-


Syntax: (a) double variable=Double.parseDouble(String);

(b) double variable=Double.valueOf(String);

e.g:- double n; String s= "24.315467398";

n=double.parse Double(s); or

n=Double valueOf(s);
Thus, the variable n results in 24.315467398 (without quotes).

CONVERSION FROM PRIMITIVE TYPE DATA TO STRING

Integer type data to String type :-

Syntax: String variable=Integer.toString(int);

int n=24; String s;

s=Integer.toString(n);

Here, an integer type number (n=24), gets converted to a String. Hence, s= "24". Now,
you can't perform any mathematical operations with the variable's'.

Long type data to String type :-

Syntax: String variable=Long.toString(long); e.g., long n=246935; String s;

s=Long.toString(n); Here, a long type number (n=246935), gets converted to a String.


Hence, s= "246935".

Float type data to String type :-

Syntax: String variable=Float..toString(float);

float n=24.35; String s;

s=Float.toString(n);

Here, a floating type number (n=24.35), gets converted to String. Hence, s= "24.35".

Double type data to String type :-

Syntax: String variable=Double.toString(double);

double n=24.3452781; String s;

s=Double.toString(n);
Here, a double type number (n=24.3452781), gets converted to String. Hence, s=
"24.3452781".

You might also like