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