String Manipulation Flashcards

1
Q

Strings are immutable which means?

A

It means that once a String object is created, it cannot be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Concatenation is…..

A

Concatenation is a fancy word for combining things together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How could the following be simplified using string concatenation?

String employeeAlias = “abcdefg”;
int level = 3;
System.out.print(employeeAlias);
System.out.print(“ is currently a level “);
System.out.print(level);
System.out.println(“.”);

A

String employeeAlias = “abcdefg”;
int level = 3;

System.out.println(employeeAlias + “ is currently a level “ + level + “.”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens when one of the values in a statement using a + is a string?

A

Java interprets + as a string concatenation and even if the other value isn’t a string it will be converted to one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Java will not allow string literals to be separated on multiple lines. How can we break up a string named hamlet to display on 3 separate lines as shown below?
To be, or not to be: that is the question:
Whether ‘tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,

A

String hamlet = ““To be, or not to be: that is the question:” +
“Whether ‘tis nobler in the mind to suffer” +
“The slings and arrows of outrageous fortune,”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can we sum the values 16 and 22 and display that sum on the same line with “Sum:” (as below) using concatenation?
Sum: 38

A

System.out.println(“Sum: “ + (16 + 22));
The parenthesis allow the addition to occur before concatenation occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the syntax using the string variables jobTitle and level to create the output with concantenation:
My level and job title are title, level.

A

System.out.println(“My level and job title are “ + jobTitle + “, “ + level + “.”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the String.format() methods general syntax?

A

String.format(“stringToBeFormated”, values…);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the syntax to use the following variables and the String.format() method to create the output below?

String studentId = “Alen345”;
int classRank = 68;

Alen345 has a class ranking of 68 for the 2022 school year.

A

String formatString = String.format(“%s has a class ranking of %d for the 2022 school year.”, studentId, classRank);
System.out.println(formatString);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What what data types do the following data type indicators expect when using the String.format() method?
s
d
f
n

A

s - is for string variables
d - is for decimal integers (byte, short, int, long)
f - is for floating point (float, double)
n - adds a new line where it is placed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many parts are there to a format specifier? How many of those parts are required?

A

6 parts of which 2 are required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the % indicator do in the String.format() method?

A

% indicates the start of a format specifier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the argument index ($) allow us to do within the String.format() method? What is the general syntax for this argument?

A

The argument index allows us to use the values in any order and even multiple times throughout our string.
The syntax is the value’s number (in the order in which it is listed with the other values) and then the $ symbol.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the six parts to a format specifier?
%
$
F
W
P
DTI

A

% Start
$ Argument Index
Flags
Width
Precision
Data Type Indicator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What do flags allow us to do within the String.format() method?

A

Flags indicate special formatting that should be used such as:

Adding comma separators in numerical values
Wrapping negative numerical values in parentheses instead of
preceding with a minus sign
Using arguments from the last format specifier
Always including a sign(+ or -) for a numerical value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What special format do the following flag symbols indicate?
,
(
0
<
+

A

, – adds comma separators in numerical values
( – wraps negative numerical values in parentheses instead of
preceding with a minus sign
0 – used with width to have zeros as padding
< – uses the argument from the last format specifier
+ – always include a sign(+ or -) for a numerical value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

int shareBalance = -10000;
What is the syntax use flags to output shareBalance as (10,000)?

A

int shareBalance = -10000;
String flagShareBalance = String.format(“%,(d”, shareBalance);
System.out.println(flagShareBalance);

18
Q

int shareBalance = -10000;
double bankBalance = 50;
What is the syntax using flags and minimum width to output shareBalance as (10,000) and bankBalance as 50.00 so they are evenly right justified on 2 separate lines?

A

int shareBalance = -10000;
double bankBalance = 50;
String flagShareBalance = String.format(“%,(8d%n%8.2f”,
shareBalance, bankBalance);

19
Q

what does a number followed by the $ symbol indicate as a format indicator? example: 2$

A

The number before the symbol $ indicates the value to use.

20
Q

What is the syntax to create a StringBuilder object named buildStr?

A

StringBuilder buildStr = new StringBuilder();

21
Q

What is the syntax to create a StringBuilder object named buildStr using the constructor to pass in the initial string named startingString?

A

StringBuilder buildStr = new StringBuilder(startingString);

22
Q

If we created a StringBuilder object named buildStr what is the syntax to add the String moreString?

A

buildStr.append(moreString);

23
Q

What is the syntax to finalize and print out our StringBuilder named buildStr as doneBuild and print it out.

A

String doneBuild = buildStr.toString();
System.out.println(doneBuild);

24
Q

What is the syntax to create a StringBuilder object named buildStr using the constructor to pass in the initial text “This isn’t so hard!” and output the result?

A

StringBuilder buildStr = new StringBuilder(“This isn’t so hard!);
String doneBuild = buildStr.toString();
System.out.println(doneBuild);

25
Q

What is the general syntax for the StringBuilder insert method on a StringBuilder named buildStr?

A

buildStr.insert(index, value);
Where index is the start location of the insert and value is a variable or value.

26
Q

Chaining appends can help improve……

A

Chaining appends can help improve readability.

27
Q

A substring must be……..

A

A substring must be contiguous.

28
Q

What is the general syntax when using indexOf on a string named ourString?

A

ourString.indexOf(substring, fromIndex);
Where:
ourString is the main string we are searching
substring is the characters we are looking for
fromIndex is, an optional, parameter to specify where to start looking (The index returned is still based on the entire main string not relative to the fromIndex)

29
Q

If the substring we are searching for using indexOf isn’t in our main string or the portion of the main string we are searching what will be returned?

A

If the substring is not in the string searched, the indexOf method will return -1.

30
Q

What is the syntax to pull the website name from the string webSite and save it to the variable siteName.
String webSite = “www.bestreviews.com”;

A

String webSite = “www.bestreviews.com”;
String siteName = webSite.substring(4, 15);
OR
int start = webSite.indexOf(“www.”) + 4; // add 4 to skip past “www.”
int end = webSite.lastIndexOf(“.”); // find the last occurrence of “.”
String siteName = webSite.substring(start, end);
System.out.println(siteName);

31
Q

What are the 2 general syntax when using substring on a string named ourString?

A

ourString.substring(startindex);
ourString.substring(startindex, endindex);

32
Q

What is the syntax to pull the website name from the string webSite and save it to the variable siteName using the indexOf to find to start and end points?
String webSite = “www.bestreviews.com”;

A

String webSite = “www.bestreviews.com”;
String siteName = webSite.substring((webSite.indexOf(“.”)) + 1, (webSite.indexOf(“.com”))

33
Q

What is the syntax to get the index of “banana” for the array fruit?

A

Arrays.asList(fruit).indexOf(“banana”)
We also need to import the Arrays package to use .asList (import java.util.Arrays;

34
Q

how can we ignore case when comparing strings?

A

The equalsIgnoreCase() method compares two strings for equality while ignoring the case of the characters in the strings.

35
Q

Does .format width need to consider the size of the variables to get them to line up?

A

no, the width is the total space and isn’t reliant on the size of the variables we are trying to line up.

36
Q

Is the default layout in String.format to the left or right when using width

A

By default, when using the String.format() method in Java, the formatted string is aligned to the right within the specified field width. To align the formatted string to the left within the specified field width, you can use the - flag as follows:

37
Q

what’s the order of the six parts of String.format

A

The % character to indicate the start of a format specifier.
The argument index (optional), which specifies the index of
the argument to use. If omitted, the arguments are used
in the order they appear in the argument list.
Flags (optional), such as 0, -, ,, <, +, etc.
Minimum width (optional), which specifies the minimum
number of characters to use for the output.
Precision (optional), which specifies the number of decimal
places or characters to use for the output.
Data type indicator, such as s, d, f, etc.

38
Q

is .indexOf() case sensitive?

A

Yes, the .indexOf() method in Java is case-sensitive. If you want to perform a case-insensitive search, you can use the .toLowerCase() or .toUpperCase() methods to convert both the string and the search term to the same case before calling .indexOf().

39
Q

How can we find the last index of something in a string?

A

In Java, you can find the last index of a specific character or substring within a string using the .lastIndexOf method.

The .lastIndexOf method returns the index of the last occurrence of the specified character or substring within the string. If the character or substring is not found, the method returns -1.

40
Q

What will this code print out?
String text = “The quick brown fox jumps over the lazy dog.”;
int index = text.indexOf(“fox”, 10);
System.out.println(index);

A

The output of this code snippet would be 16. Remember, the result from .indexOf will still be based on the original string not the start index of the search.