String Class

String Class

Better explanation needed.

Making a String Object

Unlike most objects, Strings can be initialized like a primitive type, or like a regular object:

String s = "whyDoSephardisExist";

is the same as:
String s = new String("whyDoSephardisExist");

Concatenation

The concatenation operator is +. One can use concatenation to combine mulitiple Strings into one longer String. You can also concatenate a primitive type to a String, but the primitive type will function as a string.

int weight = 165;
int age = 31;
int height1 = 5;
int height2 = 10;
String a = "Mr. Markowitz weighs ";
String b = " pounds, is ";
String c = " years old, and is ";
String d = " feet ";
String e = " inches tall."
 
String truth = a + weight + b + age + c + height1 + d + height2 + e;

The string truth prints out - Mr. Markowitz weighs 165 pounds, is 31 years old, and is 5 feet 10 inches tall.

Escape Characters

Special characters that include a backslash "\". Each character doublet is only considered as a single character.

\b
Backspace
\f
Formal Feed
\n
New Line
\r
Carriage Return
\t
Tab
\'
Apostrophe
\"
Quotation Marks
\\
Backslash

Important String Methods

Equals

To compare two Strings to see if they are identical you would use the equals method. DO NOT USE == TO COMPARE STRINGS because doing so only comapre the Strings' references not their actual content.

String s = new String("oops");
String t = new String("oops");
 
if (s == t){
    .....
}

This if statement will return false even though the content of the two Strings are the same. Since two different Strings with two different references were initialized, comparing them with == will always be false because they do not share references. Instead use:
String s = new String("oops");
String t = new String("oops");
 
if (s.equals(t)){
    .....
}

Also useful is equalsIgnoreCase(String str).

Comparison

The compareTo method compares two Strings lexicographically. If string1 precedes string2 in the dictionary then the method will return an integer less than 0. If string1 is after string 2 in the dictionary then the method will return an integer greater than 0. If they are identical the method will return 0. In comparison all digits precede all capital letters which precede all lowercase letters.

String string1 = "apple";
String string2 = "zebra";
 
if (string1.compareTo(string2) < 0){
    .....
}

This if statement will return true because "apple" precedes "zebra" lexicographically. The compareToIgnoreCase method works the same way, except it ignores the case, as the name of the method shows.

Length - int length()

The length method returns the length of the given String.

String s = "apple";
 
s.length();

s.length() will return 5 because apple contains five letters.

CharacterValue - char charAt(int Index)

The charAt method is used to find the what character is at a specific index in the string.

"please help me!".charAt(5);

This returns 's'.

Substrings - String substring(int startIndex)

This substring method returns a new string that is a substring of the given string. The substring starts at the passed startIndex of the original string and continues to the end of the string.

"JokeStore".substring(4);

This returns "Store".

Substring - String substring(int startIndex, int endIndex)

This substring method also returns a substring of the given string. The substring begins at the passed startIndex and ends at the passed endIndex, not including the letter at endIndex. The length of the returned substring will be endIndex - startIndex.

"JokeStore".substring(2,5);

This returns "keS".

Index Of - int indexOf(String str)

The indexOf method returns the index of the first occurence of str in a given string.

"JokeStore".indexOf("Store");

This returns 4 because the substring Store begins at index 4 of the original string.

Other useful String Methods

  • toUpperCase()- as expected.
  • toLowerCase()- also as expected.
  • replace(CharSequence target, CharSequence replacement) - replaces the target sequence with the replacement sequence
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License