You are on page 1of 16

String Manipulation

CC3 CLUSTER 2ND SEM A.Y. 2019-2020


String
 Strings,
which are widely used in Java programming, are a
sequence of characters.
 InJava programming language, strings are treated as
objects.
String Class
 The
Java platform provides the String class to create and
manipulate strings.
 TheString class has 11 constructors that allow you to
provide the initial value of the string using different
sources, such as an array of characters.
String Class Constructors
 String()
 Allocates a new String containing no characters.

 String(String value)
 Allocates a new string that contains the same sequence of characters as the string
argument.

 String(char[] value)
 Allocates a new String so that it represents the sequence of characters currently
contained in the character array argument.
String Class Methods
Return Type Method Name Function
char charAt(int index) Returns the character at the specified index.
String concat(String str) Concatenates the specified string to the end of this string.

Boolean endsWith(String suffix) Tests if this string ends with the specified suffix.
Boolean startsWith(String prefix) Tests if this string starts with the specified prefix.
int length() Returns the length of this string.
String replace(char oldChar, char Returns a new string resulting from replacing all occurrences of
newChar) oldChar in this string with newChar.
String toLowerCase() Converts all of the characters in this String to lower case using the
rules of the default locale, which is returned by Locale.getDefault.
String toUpperCase() Converts all of the characters in this String to upper case using the
rules of the default locale, which is returned by Locale.getDefault.
String substring(int beginIndex) Returns a new string that is a substring of this string.
String trim() Removes white space from both ends of this string.
String[] split() Breaks a given string around matches of the given regular
expression.
concat(String str)
 Parameters:
 str - the String that is concatenated to the end of this String.
 Returns:
 a string that represents the concatenation of this object's characters followed by the
string argument's characters.
charAt(int index)
 Parameters:
 index - the index of the character.
 Returns:
 the character at the specified index of this string. The first character is at index
0.
 Throws:
 StringIndexOutOfBoundsException - if the index is out of range.
endsWith(String suffix)
 Parameters:
 suffix - the suffix.
 Returns:
 true if the character sequence represented by the argument is a suffix of the character
sequence represented by this object; false otherwise.
startsWith(String prefix)
 Parameters:
 prefix - the prefix.
 Returns:
 true if the character sequence represented by the argument is a prefix of the character
sequence represented by this string; false otherwise.
length()
 Returns:
 the length of the sequence of characters represented by this object.
replace(char oldChar, char newChar)
 Parameters:
 oldChar - the old character.
 newChar - the new character.
 Returns:
 a string derived from this string by replacing every occurrence of oldChar with newChar.
toLowerCase()
 Returns:
 the string, converted to lowercase.
toUpperCase()
 Returns:
 the string, converted to uppercase.
substring(int beginIndex)
 Parameters:
 beginIndex - the beginning index, inclusive.
 Returns:
 the specified substring.
trim()
 Returns:
 this string, with white space removed from the front and end.
split()
 Parameters:
 regex - a delimiting regular expression
 Limit - the result threshold
 Returns:
 An array of strings computed by splitting the given string.
 Throws:
 PatternSyntaxException - if the provided regular expression’s syntax is invalid.

You might also like