String Manipulation Flashcards
Strings are immutable which means?
It means that once a String object is created, it cannot be changed.
Concatenation is…..
Concatenation is a fancy word for combining things together.
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(“.”);
String employeeAlias = “abcdefg”;
int level = 3;
System.out.println(employeeAlias + “ is currently a level “ + level + “.”);
What happens when one of the values in a statement using a + is a string?
Java interprets + as a string concatenation and even if the other value isn’t a string it will be converted to one.
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,
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 can we sum the values 16 and 22 and display that sum on the same line with “Sum:” (as below) using concatenation?
Sum: 38
System.out.println(“Sum: “ + (16 + 22));
The parenthesis allow the addition to occur before concatenation occurs.
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.
System.out.println(“My level and job title are “ + jobTitle + “, “ + level + “.”);
What is the String.format() methods general syntax?
String.format(“stringToBeFormated”, values…);
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.
String formatString = String.format(“%s has a class ranking of %d for the 2022 school year.”, studentId, classRank);
System.out.println(formatString);
What what data types do the following data type indicators expect when using the String.format() method?
s
d
f
n
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 many parts are there to a format specifier? How many of those parts are required?
6 parts of which 2 are required.
What does the % indicator do in the String.format() method?
% indicates the start of a format specifier
What does the argument index ($) allow us to do within the String.format() method? What is the general syntax for this argument?
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.
What are the six parts to a format specifier?
%
$
F
W
P
DTI
% Start
$ Argument Index
Flags
Width
Precision
Data Type Indicator
What do flags allow us to do within the String.format() method?
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
What special format do the following flag symbols indicate?
,
(
0
<
+
, – 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
int shareBalance = -10000;
What is the syntax use flags to output shareBalance as (10,000)?
int shareBalance = -10000;
String flagShareBalance = String.format(“%,(d”, shareBalance);
System.out.println(flagShareBalance);
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?
int shareBalance = -10000;
double bankBalance = 50;
String flagShareBalance = String.format(“%,(8d%n%8.2f”,
shareBalance, bankBalance);
what does a number followed by the $ symbol indicate as a format indicator? example: 2$
The number before the symbol $ indicates the value to use.
What is the syntax to create a StringBuilder object named buildStr?
StringBuilder buildStr = new StringBuilder();
What is the syntax to create a StringBuilder object named buildStr using the constructor to pass in the initial string named startingString?
StringBuilder buildStr = new StringBuilder(startingString);
If we created a StringBuilder object named buildStr what is the syntax to add the String moreString?
buildStr.append(moreString);
What is the syntax to finalize and print out our StringBuilder named buildStr as doneBuild and print it out.
String doneBuild = buildStr.toString();
System.out.println(doneBuild);
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?
StringBuilder buildStr = new StringBuilder(“This isn’t so hard!);
String doneBuild = buildStr.toString();
System.out.println(doneBuild);