The String Methods
As mentioned, the String Class contains a large number of methods. Later, we'll see how to look up classes like String in the Official Java Documentation. I also created a String reference with example code to help beginners. However, since documentation can still be confusing for a beginner, this node will cover the most common String methods!
concatenate
Strings can be concatenated, or combined, with the +
operator. For example:
String firstName = "Jim";
String lastName = "Jones";
String fullName = firstName + lastName;
System.out.println(fullName);
This will print out:
JimJones
The +
operator is actually a shortcut for the concat()
method. So the 3rd line in the above code can equivalently be written as:
String fullName = firstName.concat(lastName);
There's no reason to use the longer form, but you should realize +
is really just concat()
, an ordinary method of String.
Immutability
Unlike instances of most classes, Strings in Java are immutable, which means that a String cannot change after it has been created. Methods that look like they modify a String really just return a new String as a value. For example, the concat()
method above does not change the initial String firstname
, which is why a new variable fullName
was needed to store the result. If you want to change the value of the current String, you can create a new String with the same variable name. For example:
String name = "Jim";
name = name.concat("Jones");
name
now equals "JimJones".
charAt
charAt(i)
- This method returns the char
located at position i
in the String. Strings are numbered like arrays, so the first letter in String str
is at position 0, and the last letter is at position str.length()-1
. For example, the following code grabs the characters 'a', 'b' and 'e':
//01234
String alpha = "abcde";
char ay = alpha.charAt(0); // 'a'
char be = alpha.charAt(1); // 'b'
char ee = alpha.charAt( alpha.length()-1 ); // 'e'
substring
substring(startIndex, endIndex)
- When this method is called on a String str
, it returns a new String which is a substring of str
. The substring begins at the given beginIndex
and extends up to the character at endIndex
(but not including that character). For example:
//0123456789
String pangram = "The five boxing wizards jump quickly.";
String ef = pangram.substring(2,5); // "e f"
String word2 = pangram.substring(4, 8); // "five"
System.out.println(ef);
System.out.println(word2);
This will print out:
e f five
substring()
can also take one parameter, which means it will return a new String that begins at the index and goes to the end of the String. For example:
System.out.println( pangram.substring(9) );
will print out the pangram
String except for the first 8 characters:
boxing wizards jump quickly.
equality
As seen before, to compare two primitive data types to see if they are equal, you use ==
. For example, the following code will print out true:
int num1 = 5;
int num2 = 5;
System.out.println(num1 == num2);
Do not use ==
to check if two Strings have the same value! It will not return reliable results. Instead, you need to use the equals
method. The following code will print out true:
String word1 = "apple";
String word2 = "apple";
System.out.println( word1.equals(word2) );
Note that equals()
is case-sensitive. The following code will print out false:
String name1 = "Jim";
String name2 = "jim";
System.out.println( name1.equals(name2) );
(To check if the letters are the same and ignore case, you can use the equalsIgnoreCase()
method.)
String challenges
Once you've gone through this node (and the next one), you can practice your String Know-how in the module String Programming Practice.
Task
You should continue doing the tasks for this project within BlueJ.
- Create a method
addToNote
in your Note class that takes one String parameter and adds it to the Note content. - Create a method
summarize
that returns a String containing the first 20 characters of the Note's content. If the content is less than 20 characters, it should return the entire content instead.
Challenge
You are given the following code:
String word = "craftsmanship";
Write one line of code that declares the char c
and uses charAt
to set its value to m
.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Victoria Holland
Jan 31, 4:51 AMI'm having trouble with the BlueJ task. I've managed to get the tests to succeed for TheStringClassTest, but when I run the tests for TheStringMethodsTest, it fails. This is the code I've written for the addToNote and summarize methods:
Learneroo
Jan 31, 10:09 AMWhen you click on the test result in BlueJ, it will show you what your method returned and what it expected. This can help you find your error.
Look at the definition of
substring
. What number do you want to go up to (but not including) to get the first 20 characters? (Remember that the first index is 0.)PS - you can leave out the keyword
this
, and just callgetContent()
.