0% found this document useful (0 votes)
1 views6 pages

Strings A Review

The document provides an overview of working with Strings in Java, emphasizing the use of String methods and the importance of understanding String and char data types. It includes common String methods, examples of using for loops to manipulate Strings, and practice questions to reinforce learning. Key skills highlighted include concatenation, counting characters, and creating new Strings from existing ones.

Uploaded by

willmaren01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Strings A Review

The document provides an overview of working with Strings in Java, emphasizing the use of String methods and the importance of understanding String and char data types. It includes common String methods, examples of using for loops to manipulate Strings, and practice questions to reinforce learning. Key skills highlighted include concatenation, counting characters, and creating new Strings from existing ones.

Uploaded by

willmaren01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Strings

Almost all String problems involve using the index of the letter in the word to do things. Let's
review everything we need to know:

When we work with Strings in Java, we need to know how to do the following skills:

​ Use methods from the String class to perform actions: String (Java Platform SE 7 )
​ Create a new String using pieces of the old String
​ Count or check if a String contains something

String Basics
Strings are a sequence of characters. Each letter is assigned a position known as an index.

char

H E L L O 1 1 s

0 1 2 3 4 5 6 7 8

index

A char is a different data type than String. The char is the individual letter in the String. In Java,
we interact with char and String very differently.

char String

Example

Comparing
String Methods
Understanding how to read and use documentation is the most important skill any programmer
can have.

When we call String methods, we have to perform an action on the String. So we say

String.methodName(parameters)

The method is the action being performed, and str is the String that is performing the
action. Using the example above, we would call the isEmpty() method to check the String
called str using the code below:

str.isEmpty()

isEmpty() returns a boolean value (true or false) as the return type. To use it in a program, we
would probably write something like this:

System.out.println("Enter a name: ");


String name = scan.nextLine();

if (name.isEmpty()){
​ System.out.println("Error: Please enter a name.");
}else{ …
Common String Methods
There are a lot of String methods so I will write the ones you will use most often below. There
are many other helpful methods in the documentation though.

Return Method Name Returns Example


Type

char .charAt(int index) Letter at the index int i = str.charAt(0);

String .substring(int start) String starting from start to String ans = str.substring(2);
the end

String .substring(int start, int stop) String from start to stop, String ans = str.substring(2, 5);
exclusive

int .length() Number of letters int len = str.length()

int .indexOf(String str) Find index of str in the int i = str.indexOf("a);


String. -1 if not found

int .indexOf(String str, int index) Find index of str in the int i = str.indexOf("a", 10);
String. Start looking at
index. -1 if not found

int .lastIndexOf(String str) Find the last index that str int i = str.lastIndexOf("i");
is located at

boolean .equals(String str) Is the String equal to str if (str.equals("a"))


For Loops
Many String problems involve repeating actions. Common problems involve counting, finding, or
building a new String from the old one.

Most problems involve using a for loop to generate the index so we can examine each letter of
the String and perform some action.

//Count the number of "a" in a String

int count = 0;

for (int i = 0; i < str.length(); i++){



char letter = str.charAt(i);

if (letter == 'a'){
​ count++;
}
}
//Return true if there are two identical letters next to each other

for (int i = 0; i < str.length()-1; i++){

if (str.charAt(i) == str.charAt(i+1)){
​ return true;
}
}

//Remove all z from a String

String ans = "";

for (int i = 0; i < str.length(); i++){

if (str.charAt(i) != 'z'){
​ ans += str.charAt(i);
}
}
Practice Questions
1.​ Given two strings, append them together (known as "concatenation") and print the result.
However, if the concatenation creates a double-char, then omit one of the chars, so
"abc" and "cat" yields "abcat" since there would have been two c's next to each other.

conCat("abc", "cat") → "abcat"


conCat("dog", "cat") → "dogcat"
conCat("abc", "") → "abc"

2.​ For the String word = "Hello World. Grade 11."


a.​ The number of e's in the word
b.​ The index of the second l
c.​ How many words there are in the String word
d.​ Create a new String without any punctuation
e.​ How many times two identical letters appear next to each other

3.​ Count the number of times the word 'jo' appears in a String. Print true if the number of
times "jo" appears in the String is even.

countJo("jojo") → true
countJo("jo123") → false
countJo("jojojo") → false

4.​ Write a method that will return a new String with only the first and last two letters of the
original String. If the String is 4 letters or smaller, return the original String unchanged.

changeString(“apple”) → “aple”
changeString(“joy”) → “joy”

5.​ Given a String, return a new String containing only the vowels.

​ onlyVowels("abcde") → "ae"

6.​ Given a String, without the .replace() method, print a new String changing all the "z" to
"s".

​ changeZ("zebra") → "sebra"

You might also like