Chapter 4.4 Strings Flashcards

1
Q

Define a string

A

A string is a sequence of characters.
A string value is enclosed in matching double quotes (“).

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

Strings are known as a ___ type not an ___

A

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.

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

Strings must be surrounded by ___

and they contain most characters except:

A
  1. Must be surrounded double quotes.

String literals (values) have the quotation marks removed when displayed.

  1. 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.

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

Strings can have up to ___ characters

A

Potentially up to 2,147,483,647 characters is allowed.

The minimum number of characters is zero “”, which is called the empty string.

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

Backspace

Escape sequence:
Unicode:
Decimal value:

A

\b
\u0008
8

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

Tab

Escape sequence:
Unicode:
Decimal value:

A

\t
\u0009
9

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

Linefeed

Escape sequence:
Unicode:
Decimal value:

A

\n
\u000C
10

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

Formfeed

Escape sequence:
Unicode:
Decimal value:

A

\f
\u000C
12

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

Carriage return

Escape sequence:
Unicode:
Decimal value:

A

\r
\u000D
13

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

Backlash

Escape sequence:
Unicode:
Decimal value:

A


\u005C
92

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

Double quote:

Escape sequence:
Unicode:
Decimal value:

A

"
\u0022
34

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

Syntax for a simple method for String objects

A

referenceVariable.methodName(arguments)

ex:
String message = “Welcome to Java”;
System.out.println(“The length of “ + message + “ is “ +message.length());

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

What is the String method to return the number of characters in the String

A

length() or length() - 1

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

What is the String method to return the character at the specified index from this string

A

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.

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

What is the String method to return a new String that concatenates

A

concat(s1)

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

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

A

toUpperCase()
toLowerCase()

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

What is the String method to return a new string with whitespace characters trimmed on both sides

A

trim()

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

Java uses the + operator for both addition and concatenation.

A

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.

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

Reading a String from the console

A

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);

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

You can use the ___ method to read an entire line of text.

A

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 thepro­gram.

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

Comparing Strings: returns true if this String is equal to string s1

A

equals(s1)

22
Q

Comparing Strings: returns true if this string is equal to string s1

A

equalsIgnoreCase(s1)

23
Q

Return an integer greater than 0, equal to 0, or less than 0 to indicate whether this string is greater than, equal to, or less than s1

A

compareTo(s1)

24
Q

Same to compareTo except that the comparison is case sensitive

A

compareToIgnoreCase(s1)

25
Q

Returns true if this String starts with the specified prefix

A

startsWith(prefix)

26
Q

Returns true if this string ends with the specified suffix

A

endWith(suffix)

27
Q

Returns this substring that begins with the character at the specified beginIndex and extends to the end of the string

A

substring(beginindex)

28
Q

Returns this String’s substring that begins at the specified beginIndex and extends to the character at the endIndex is not part of the substring

A

substring(beginIndex, endIndex)

29
Q

Returns the index of the of the first occurrence of the ch in the string

A

indexOf(ch)

30
Q

Returns the index of the first occurrence of ch after fromIndex in the String, Returns -1 if not matched

A

indexOf(ch, fromIndex)

31
Q

Returns the index of the first occurrence of the of string s in the String. Returns -1 if not matched

A

indexOf(s)

32
Q

Returns the index of the first occurrence of String s in this string after fromIndex. Returns true if not matched.

A

indexOf(s, fromIndex)

33
Q

Returns the index of the last occurrence of ch in the String. Returns -1 if not matched.

A

lastIndexOf(ch)

34
Q

Returns the index of the last of the last occurrence of ch before fromIndex in this string. Return -1 if not matched.

A

lastIndexOf(ch, fromIndex)

35
Q

Return the index of the last occurrence of string s. Returns -1 if not matched.

A

lastIndexOf(s)

36
Q

Returns the index of the last occurrence of string s before fromIndex. Returns -1 if not matched.

A

lastIndexOf(s, fromIndex)

37
Q

To convert a string into an int value, use the ___ method:

A

To convert a string into an int value, use the Integer.parseInt method:

int intValue = Integer.parseInt(intString);

38
Q

To convert a string into a double value, use the ___ method:

A

To convert a string into a double value, use the Double.parseDouble method:

double doubleValue = Double.parseDouble(doubleString);

If the string is not a numeric string, conversion would cause a runtime error.

39
Q

To convert a number into a string use the string concatenating operator as follows:

A

To convert a number into a string use the string concatenating operator as follows:

String str = intValue + “ “ + doubleValue;

40
Q

Formatting output: how to display numbers in a certain format (Strings)

A

Often, it is desirable to display numbers in a certain format. To do that we can use the printf statement:

System.out.printf(format, items);

Where format is a string that may consist of substrings and format specifiers.

41
Q

What type does an item have to be to format output

A
  1. An item may be a numeric value, character, boolean value, or a string.
42
Q

What does a format specifier specifier do

A

A format specifier specifies how an item should be displayed.

43
Q

How to format output

A

Each specifier begins with a percent sign (%).

To output a literal % in the format string, use %%

Items must match the format specifiers in order, in number, and in exact type.

You can specify the field width and precision in a format specifier.

By default, the output is right justified. You can put the minus sign (−) in the format specifier to specify that the item is left justified in the output within the specified field.

44
Q

%b output and example

A

boolean
T or F

45
Q

%c output and example

A

a character
‘a’

46
Q

%d output and example

A

a decimal integer
200

47
Q

%f output and example

A

a floating point number
45.46000

48
Q

%e output and example

A

a number in standard scientific notation
4.556000e+01

49
Q

format syntax

A

1.2f

1 = field width
2 = precision
f = conversion code

50
Q

Character method to: return the lowercase of the specified character

A

Math.pow(a, b)