Chapter 4.4 Strings Flashcards
Define a string
A string is a sequence of characters.
A string value is enclosed in matching double quotes (“).
Strings are known as a ___ type not an ___
The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable.
Strings must be surrounded by ___
and they contain most characters except:
- Must be surrounded double quotes.
String literals (values) have the quotation marks removed when displayed.
- Strings can contain most characters except enter, backspace, tab, and backslash.
These characters must be escaped by using an escape sequence which consists of the escape character () followed by a character or a combination of digits.
Strings can have up to ___ characters
Potentially up to 2,147,483,647 characters is allowed.
The minimum number of characters is zero “”, which is called the empty string.
Backspace
Escape sequence:
Unicode:
Decimal value:
\b
\u0008
8
Tab
Escape sequence:
Unicode:
Decimal value:
\t
\u0009
9
Linefeed
Escape sequence:
Unicode:
Decimal value:
\n
\u000C
10
Formfeed
Escape sequence:
Unicode:
Decimal value:
\f
\u000C
12
Carriage return
Escape sequence:
Unicode:
Decimal value:
\r
\u000D
13
Backlash
Escape sequence:
Unicode:
Decimal value:
\u005C
92
Double quote:
Escape sequence:
Unicode:
Decimal value:
"
\u0022
34
Syntax for a simple method for String objects
referenceVariable.methodName(arguments)
ex:
String message = “Welcome to Java”;
System.out.println(“The length of “ + message + “ is “ +message.length());
What is the String method to return the number of characters in the String
length() or length() - 1
What is the String method to return the character at the specified index from this string
charAt(index)
The charAt(index) method can be used to retrieve a specific character in a string s,
where the index is between 0 and length()–1.
What is the String method to return a new String that concatenates
concat(s1)
What is the String method to return a new string with all letters uppercase
What is the String method to return a new string with all letters lowercase
toUpperCase()
toLowerCase()
What is the String method to return a new string with whitespace characters trimmed on both sides
trim()
Java uses the + operator for both addition and concatenation.
If one of the operands is a nonstring (e.g., an integer), the nonstring value is converted into a string and concatenated with the other string.
If neither of the operands is a string, the plus sign (+) is the addition operator that adds two numbers.
Reading a String from the console
To read a string from the console, invoke the next() method on a Scanner object.
The next() method reads a string that ends with a whitespace character.
Scanner input = new Scanner(System.in);
System.out.print(“Enter three words separated by spaces: “);
String s1 = input.next();
String s2 = input.next();
String s3 = input.next();
System.out.println(“s1 is “ + s1);
System.out.println(“s2 is “ + s2);
System.out.println(“s3 is “ + s3);
You can use the ___ method to read an entire line of text.
You can use the nextLine() method to read an entire line of text. The nextLine() method reads a string that ends with the Enter key pressed.
Scanner input = new Scanner(System.in);
System.out.println(“Enter a line: “);
String s = input.nextLine();
System.out.println(“The line entered is “ + s);
Note:
To avoid input errors, be cautious using line-based input after a token-based input in theprogram.