04 Strings
04 Strings
Erciyes University
Department of Computer Engineering
Method Description
length() returns the number of characters in a string
charAt(index) returns the character at the specified index from this string
concat(String) returns a new string that concantenate this string with argument string
toUpperCase() returns a new string with all letters in uppercase
toLowerCase() returns a new string with all letters in lowercase
trim() returns a new string with whitespace characters trimmeds
Method Description
equals(String s1) returns true if this string equals to string s1
equalsIgnoreCase(String) returns true if this string equals to string s1 (it is case insensetive)
returns an integer greater than 0 if this string greater than s1
compareTo(String) returns 0 if this string equals to s1
returns an integer less than 0 if this string less than s1
compareToIgnoreCase(String) same as compareTo() except that comparison is case insensetive
startsWith(prefix) returns true if this string starts with specified prefix
endsWith(suffix) returns true if this string ends with specified postfix
Note: "==" operator only checks the referential equality of two Strings, meaning if
they reference the same object or not.
Method Description
returns this string’s substring that begins with the character
substring(beginIndex)
at the specified beginIndex and extends to the end of the string
returns this string’s subtrings that begins at the specified
substring(beginIndex, endIndex) beginIndex and extends to the character at index endIndex-1.
Note that the character at endIndex is not part of the substring
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
message J A V A P R O G R A M M I N G
Method Description
returns index of the first occurence of ch in the string.
indexOf(char ch)
return -1 if not matched
returns index of the first occurence of ch after fromIndex in the string.
indexOf(ch, fromIndex)
return -1 if not matched
returns index of the first occurence of s in the string.
indexOf(String s)
return -1 if not matched
returns index of the first occurence of s after fromIndex in the string.
indexOf(s, fromIndex)
return -1 if not matched
returns index of the last occurence of ch in the string.
lastIndexOf(ch)
return -1 if not matched
returns index of the last occurence of ch before fromIndex in the string.
lastIndexOf(ch, fromIndex)
return -1 if not matched
returns index of the last occurence of s in the string.
lastIndexOf(s)
return -1 if not matched
returns index of the last occurence of s before fromIndex in the string.
lastIndexOf(s, fromIndex)
return -1 if not matched
Number to string:
I You can use toString() methods of Wrapper Classes (e.g. Integer)
I You can use static valueOf() method of String class
int value = 12;
String intString = Integer . toString ( value ); // "12"
s s
:String :String This string object is
now unreferenced
String object for "Java" String object for "Java"
Contents cannot be
changed :String
s1 s2
:String :String
s3
Interned string object A string object for
for "Welcome to Java" "Welcome toJava"
Method Description
StringBuilder append(aPrimitiveType v)
Appends the argument to this string builder
append(char[] str)
The data is converted to a string
StringBuilder append(char[] str, int offset, int len)
before the append operation takes place
StringBuilder append(String s)
StringBuilder delete(int start, int end) Deletes characters from start to end
StringBuilder deleteCharAt(int index) Deletes the character located at index
Replaces the characters from start to end in this string
StringBuilder replace(int start, int end, String s)
builder with the specified string
void setCharAt(int index, char c) Replaces the specified character in this string builder
StringBuilder insert(int offset, aPrimitiveType v) Inserts a value converted to String int to this builder
StringBuilder insert(int index, char[] str, Inserts a subarray of data in the array to the builder
int offset, int len) at the specified index
StringBuilder insert(int offset, char[] str) Inserts data into this builder at the position offset
StringBuilder insert(int offset, String s) Inserts string into this builder at the position offset
StringBuilder reverse() Reverses the sequence of characters in this string builder.
Method Description
int capacity() Returns the capacity of this string builder
int length() Returns the number of characters of this string builder
void setLength(int newLength) Sets a new length in this builder
Returns a string that contains the character sequence
String toString()
in the builder
void trimToSize() Reduces the storage size used for string builder
String substring(int start) Returns a substring starting at start
String substring(int start, int end) Returns a substring from start to end -1
stringBuilder . append ( " Java " ); // changes to " Welcome to Java Java "
stringBuilder . insert (11 , " HTML and " ); // " Welcome to HTML and Java "
stringBuilder . delete (8 , 11); // changes to " Welcome Java "
stringBuilder . deleteCharAt (8); // changes to " Welcome o Java "
stringBuilder . reverse (); // changes to " avaJ ot emocleW ".
stringBuilder . replace (11 , 15 , " HTML " ) // changes to " Welcome to HTML "
stringBuilder . setCharAt (0 , ’w ’) // sets to " welcome to Java "
Problem
A palindrome is a word, number, phrase, or other sequence of characters
which reads the same backward as forward. Given a string, determine if it
is a palindrome, considering only alphanumeric characters and ignoring
cases. For example, A man, a plan, a canal: Panama is a palindrome.
Solution
Write methods to reverse the given string and filter the non-alphanumeric
characters. You can use equals() method to check if two strings are equal.