String Operations in Java

Remember in the previous lesson, you had been told that Java treats single characters and a group of characters (termed as Strings) differently.

Let’s start with declaring a character and a string:

To declare a Character:

Syntax:

<Data Type> <variable name> = <character value>;

E.g.:

char x='n';

Similarly to declare a String:

Syntax:

<Data Type> <variable name> = <String Value>;

E.g.:

String a="Iron Man";

[Note that the value of a character variable has to be always enclosed within single inverted commas whereas the value of a String Variable has to be always within double-inverted commas.]

 

Character Functions:

  • isLetter():

Function: Checks whether given argument is an alphabet or not.

Returns: Respective boolean value of true or false.

Syntax:

boolean <variable name> = Character.isLetter(<argument>);

Example:

boolean p=Character.isLetter('N')

will return true.

boolean f = Character.isLetter('10') 

will return false as it is a numeric value.

  • isDigit():

Function: Checks whether given argument is a digit or not.

Returns: Respective boolean value of true or false.

Syntax:

boolean <variable name> = Character.isDigit(<argument>);

Example:

boolean p=Character.isDigit('N')

will return false.

boolean f = Character.isDigit('10')

will return true.

 

  • isLetterOrDigit():

Function: Checks whether given argument is either an alphabet or a digit.

Returns: Respective boolean value of true or false.

Syntax:

boolean <variable name> = Character.isLetterOrDigit(<argument>);

Example:

boolean p=Character.isLetterOrDigit('N')

will return true.

boolean f = Character.isLetterOrDigit('$')

will return false as it is a special symbol.

 

  • isWhiteSpace():

Function: Checks whether given argument is a white blank or not.

Returns: Respective boolean value of true or false.

Syntax:

boolean <variable name> = Character.isWhiteSpace(<argument>);

Example:

boolean p=Character.isWhiteSpace ('N')

will return false.

boolean f = Character.isWhiteSpace (‘ ’)

will return true

 

  • isUpperCase():

Function: Checks whether given argument is a upper case alphabet or not.

Returns: Respective boolean value of true or false.

Syntax:

boolean <variable name> = Character.isUpperCase(<argument>);

Example:

boolean p=Character. isUpperCase ('N')

will return true.

boolean f = Character. isUpperCase ('x')

will return false.

 

  • isLowerCase():

Function: Checks whether given argument is a lowercase alphabet or not.

Returns: Respective boolean value of true or false.

Syntax:

boolean <variable name> = Character.isLowerCase(<argument>);

Example:

boolean p=Character.isLowerCase ('N')

will return false.

boolean f = Character.isLowerCase ('x')

will return true.

 

  • toUpperCase():

Function: Changes given argument to upper case character alphabet.

Returns: Uppercase equivalent of given argument.

Syntax:

char <variable name> = Character.toUpperCase(<argument>);

Example:

char p=Character. isUpperCase ('N')

will return N only because it is already in uppercase.

char f = Character. isUpperCase ('x')

will return X.

 

  • toLowerCase():

Function: Changes given argument to lower case character alphabet.

Returns: Lowercase equivalent of given argument.

Syntax:

char <variable name> = Character.toLowerCase(<argument>);

Example:

char p=Character. isLowerCase ('N')

will return n.

char f = Character. isUpperCase ('x')

will return x since it is already in lowercase.

String Functions:

  • length():

Function: Counts the number of characters in the given string.

Returns: Length of String in integer value.

Syntax:

int <variable name> = <variable name>.length();

Example:

Suppose  z = "Tony Star"

int p=z.length();

will return 10 (including the space character).

 

  • charAt():

Function: Extracts the character at a given index position.

Returns: Returns the extracted character.

Syntax: 

char <variable name> = <variable name>.charAt(<index>);

Example:

Suppose

z = “Tony Stark”

You want to extract the 5th character of the String z.

char c=z.charAt (4) will return ‘S’ (including the space character).

Note that the indexing of the string starts from 0.

 

  • indexOf():

Function: Used to evaluate the index of a character in a string.

Returns: Index of the given character in integer.

Syntax:

int <variable name> = <variable name>.indexOf(<character>);

Example:

Suppose  z = “Tony Stark”

You want to extract the index of the character of ‘y’.

int p=z.indexOf (‘y’) will return 3 (including the space character).

Note that the indexing of the string starts from 0.

 

  • lastIndexOf():

Function: Used to calculate the last index of a character in a string.

Returns: Last index of the given character in an integer.

Syntax:

int <variable name> = <variable name>.lastIndexOf(<character>);

Example:

Suppose  z = “Hippopotamus”

You want to extract the index of the character of ‘p’.

int p=z.lastIndexOf (‘p’) will return 5 since index 5 marks the last occurrence of ‘p’.

 

  • substring():

Function: Used to extract a part of a string.

Returns: The extracted part of the string using the given index position in string value.

Syntax:

String <variable name> = <variable name>.substring(index1,index2);

Example:

Suppose  z = “Hippopotamus”

You want to extract the part of string from index 1 to 5.

String p=z.substring(1,5) will return ippo (inclusive of index 1 and exclusive of index2)

 

  • toLowerCase():

Function: Convert a string in lowercase.

Returns: Lowercase Equivalent of given string.

Syntax:

String <variable name> = <variable name>.toLowerCase();

Suppose  z = “HIppOTAmUs”

String p=z.toLowerCase() will return hippopotamus.

 

  • toLowerCase():

Function: Convert a string in lowercase.

Returns: Lowercase Equivalent of given string.

Syntax

String <variable name> = <variable name>.toLowerCase();

Suppose  z = “HIppOTAmUs”

String p=z.toLowerCase() will return hippopotamus.

 

  • toUpperCase():

Function: Convert a string in uppercase.

Returns: Uppercase Equivalent of given string.

Syntax

String <variable name> = <variable name>.toUpperCase();

Suppose  z = “HIppOTAmUs”

String p=z.toUpperCase() will return HIPPOPOTAMUS.

 

 

  • replace():

Function: Replaces a character with another character or a string with another string at all recurring occurrences in the main string.

Returns: Converted String after replacing all strings and characters.

Syntax

String <variable name> = <variable name>.toUpperCase();

Suppose  z = “HIppOTAmUs”

String p=z.toUpperCase() will return HIPPOPOTAMUS.

 

  • concat():

Function: Used to join (concatenate) two strings.

Returns: The two strings concatenated.

Syntax

String <variable name> = <variable name1>.concat(variable name2);

Suppose  z = “The Big” , y =” Bang Theory”

String p=z.concat(y) will return “The Big Bang Theory”.

 

  • equals():

Function: Used to compare the equality of two strings (case sensitive).

Returns: If both strings are same, then true is returned otherwise false.

Syntax

boolean <variable name> = <variable name1>.equals(variable name2);

Suppose  z = “The Big” , y =” Bang Theory”

String p=z.equals(y) will return false.

Suppose  z = “definite” , y =” DEFINITE”

String p=z.equals(y) will return false since case of both the words are different.

 

  • equalsIgnoreCase():

Function: Used to compare the equality of two strings (Not case sensitive).

Returns: If both strings are same, then true is returned otherwise false.

Syntax

boolean <variable name> = <variable name1>.equalsIgnoreCase(variable name2);

Suppose  z = “The Big” , y =” Bang Theory”

String p=z.equalsIgnoreCase (y) will return false.

Suppose  z = “definite” , y =” DEFINITE”

String p=z. equalsIgnoreCase (y) will return true since both the words are same irrespective of their case.

 

  • compareTo():

Function: Used to compare equality of two strings and also which of them is bigger or smaller than the other (case sensitive).

Returns: If both strings are same, returns n=0

If string1 is greater than string2, returns n>0

If string1 is smaller than string2, returns n<0

 

Syntax

boolean <variable name> = <variable name1>.compareTo(variable name2);

Suppose  z = “The Big” , y =” Bang Theory”

String p=z. compareTo (y) will return value less than 0 since string1 is smaller than string 2.

Suppose  z = “DEFINITE” , y =” DEFINITE”

String p=z. compareTo (y) will return 0 since both the strings are equal.

 

 

String Operations in Java