Strings in JAVA

Strings in JAVA

Strings in JAVA differ from strings in C as we have a String Class in JAVA and in C/C++ strings used to be an array of characters. The string variable is an object of String Class.  Strings are immutable in JAVA. We also have StringBuffer Class and String Builder Class where string objects are mutable. With the String Class there are many inbuilt methods making it easy to handle string data. The String methods are listed below after the programs.

JAVA program to convert a string from lowercase to uppercase without using string method
JAVA program to convert a string from lowercase to uppercase using string method toUpperCase()
JAVA program to convert a string from uppercase to lowercase without using string method
JAVA program to convert a string from uppercase to lowercase using string method toLowerCase()
JAVA program to find length of a string without using string method length()
JAVA program to find length of a string using string method length()
JAVA program to convert a string to a character array using toCharArray() method
JAVA program to toggle all characters of a string
JAVA program to check if the string is a palindrome
JAVA program to compare two strings without using string method equals()
JAVA program to compare two strings using string method equals()
JAVA program to compare two strings using string method equalsIgnoreCase()
JAVA program to reverse a string without using inbuilt method reverse()
JAVA program to reverse a string using reverse() method
JAVA program to concatenate two strings without using string method concat()
JAVA program to concatenate two strings using string method concat()
JAVA program to count number of vowels, consonants, digits,white spaces and special characters in a given string

 

Method Description
.length() Returns an integer value equal to length of the String
.toCharArray() String object is converted to an array of characters
.concat(String) String to be concatenated is passed to the method in brackets. Returns concatenated strings
.charAt() Returns the character at the index passed to this method in the brackets
.toLowerCase() String is converted to lower case
.toUpperCase() String is converted to upper case
.equals(String) Returns true if two strings are equal else returns false
.equalsIgnoreCase(String) Returns true if two strings are equal else returns false, only difference is it ignores the case which is upper case or lower case
.substring(int) Returns a sub-string of a string object starting from index mentioned in the brackets
.substring(int, int) Returns a sub-string of a string object starting  and ending from indices mentioned in the brackets .The index mentioned in the second parameter is excluded
.indexOf(char) Returns the index of the character passed in the brackets
.indexOf(char, int) Returns the index of the character passed in the brackets after the index of integer passed in the brackets.
.trim() Removes blank spaces from the start and end of a string
.replace(char, char) Replace all occurrences of the first character with the second character passed to the method
.compareTo(String) Compares the string object through which it is called and String object passed to this method. Returns 0 if the two strings are equal, returns positive if the first string is greater than the String object passed, else returns a negative value

 

Share Me!
  •  
  •  
  •  
  •  
  •  
  •