String and String Methods

 String is a sequence of characters.The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.

Creating a String

There are two ways to create string in Java:

  • String literal
    String s = “Master of Computer Application”;
  • Using new keyword
    String s = new String (“Master of Computer Application”);

String Methods :-

charAt(int index)
Returns the char value at the specified index.

compareTo(String anotherString)
Compares two strings .

equals(Object anObject)
Compares this string to the specified object.
concat(String str)
Concatenates the specified string to the end of this string.

contains(CharSequence s)
Returns true if and only if this string contains the specified 
sequence of char values.

length()
Returns the length of this string.

toLowerCase()
Converts all of the characters in this String to lower case 
using the rules of the default locale.

toUpperCase()
Converts all of the characters in this String to upper case 
using the rules of the default locale.

startsWith(String prefix)
Tests if this string starts with the specified prefix.
endsWith(String suffix)
Tests if this string ends with the specified suffix.

replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences
 of oldChar in this string with newChar.

isEmpty()
Returns true if, and only if, length() is 0.

Post a Comment

0 Comments