String Flashcards
It is a sequence of zero or more characters from the Unicode character set.
String
It is the sequence consisting of zero characters
Null string
What is it called when an object cannot be changed?
Immutable
Does a string change once created?
NO
If a string does not change once created, what happens instead?
Methods produce new strings from old ones
A representation for a string value within the program text
String Literal
In Java, it is a sequence of zero or more graphic characters from the Unicode character set or escape sequences, enclosed in double-quotes
String Literal
What is the syntax for string declaration?
String varname = value;
What does the following String function do?
charAt (int i)
Character at position i
What does the following String function do?
compareTo (String t)
Compare with t
What does the following String function do?
concat (String t)
Concatenation with t
What does the following String function do?
equals (String t)
Same characters as t
What does the following String function do?
equalsIgnoreCase
Same characters as t (ignoring case differences)
What does the following String function do?
indexOf (char c)
Position of the first occurence of c
What does the following String function do?
indexOf (String t)
Position of the first occurence of t
What does the following String function do?
length ()
Number of characters in String
What does the following String function do?
replace (char c, char d)
Replaces c with d
What does the following String function do?
substring (int f int t)
Substring from position f up to but not including t
What does the following String function do?
toLowerCase()
Equivalent string in all lowercase
What does the following String function do?
toUpperCase()
Equivalent string in all uppercase
What does the following String function do?
trim ()
Equivalent string without leading and trailing white space
What would the output be?
String str = “programming”;
char ch = str.charAt(2)
System.out.println(“Character at charAt(2): “ + ch);
ch = str.charAt(5)
System.out.println(“Character at charAt(5): “ + ch);
Character at charAt(2): o
Character at charAt(5): a
What would the output be?
System.out.println(“Character at charAt(2):” + ch);
String s1 = “Hello”;
String s2 = “Hello;”
String s3 = “HELLO”;
String s4 = “hello”;
System.out.println(“s1.compareTo(s2) “ + s1.compareTo(s2));
System.out.println(“s1.compareTo(s3) “ + s1.compareTo(s3));
System.out.println(“s1.compareTo(s4) “ + s1.compareTo(s4));
s1.compareTo(s2) 0
s1.compareTo(s3)
s1.compareTo(s4) -32
What value is returned if the content of the strings are the same when using the compareTo function?
0
What value is returned if the first string comes before the second in alphabetical order when using the compareTo function?
A negative value
What value is returned if the first string comes after the second in alphabetical order when using the compareTo function?
A positive value
What would the output be?
String s1 = “Hello”;
String s2 = “Programming”;
String s3 = “Enjoy”;
String s4 = “ s1.concat(s2);
String s5 = “s4.concat(s3);
System.out.println(“s1.concat(s2)» “ + s4);
System.out.println(“s4.concat(s3)» “ + s5);
s1.concat(s2)» HelloProgramming
s4.concat(s3)» HelloProgrammingEnjoy
What would the output be?
String s1 = “Hello”;
String s2 = “Hello;”
String s3 = “HELLO”;
String s4 = “hello”;
System.out.println(“s1.equals(s2) “ + s1.equals(s2));
System.out.println(“s1.equals(s3) “ + s1.equals(s3));
System.out.println(“s1.equals(s4) “ + s1.equals(s4));
s1.equals(s2) true
s1.equals(s3) false
s1.equals(s4) false
What would the output be?
String s1 = “Google classroom”;
System.out.println(“s1.indexOf(‘o’) “ + s1.indexOf(‘o’));
System.out.println(“s1.indexOf(‘c’) “ + s1.indexOf(‘c’));
System.out.println(“s1.indexOf(‘s’) “ + s1.indexOf(‘s’));
System.out.println(“s1.indexOf(‘G’) “ + s1.indexOf(‘G’));
System.out.println(“s1.indexOf(‘g’) “ + s1.indexOf(‘g’));
System.out.println(“s1.indexOf(‘x’) “ + s1.indexOf(‘x’));
s1.indexOf(‘o’) 1
s1.indexOf(‘c’) 7
s1.indexOf(‘s’) 10
s1.indexOf(‘G’) 0
s1.indexOf(‘g’) 3
s1.indexOf(‘x’) -1
What would the output be?
String s1=”The quick brown fox and the dog”
System.out.println(“s1.indexOf(“the”) “+s1.indexOf(“the System.out.println(“s1.indexOf(“Thel”)” + sindexOf(“The System.out.println(“s1.indexOf(“fox")” + s1.indexOf(“fox”));
System.out.println(“s1.indexOf(“Fox") “ + s1.indexOf(“Fox”));
System.out.println(“s1.indexOf("cat”)” + s1.indexOf(“cat”));
s1.indexOf(“the”) 24
s1.indexOf(“The”) 0
s1.indexOf(“fox”) 16
s1.indexOf(“Fox”) -1
s1.indexOf(“cat”) -1
What would the output be?
String str1=”programming”
System.out.println(“str1.replace(‘m’, ‘g’)»“+str1.replace(‘m’, ‘g System.out.println(“str1.replace("gram", "ham")»_space; “ str1.replace(“gram”, “ham”));
str1.replace(‘m’, ‘g’)» progragging str1.replace(“gram”, “ham”)»_space; prohamming
What would the output be?
String str1 = “programming”;
System.out.println(“str1.substring(1, 4): “ + str1.substring(1, 4));
System.out.println(“str1.substring(2, 7): “ + str1.substring(2, 7));
System.out.println(“str1.substring(3): “ + str1.substring(3));
str1.substring(1, 4): rog
str1.substring(2, 7): ogram
str1.substring(3): gramming
It is when a method breaks a given string around matches of the given regular expression
Split
It returns an array of strings computed by splitting the given string
Split
Method in String class
Split
It returns an array of strings after splitting an input String based on the delimiting regular expression
String[] split(Spring regex)
This limits the number of strings returned after split-up
String[] split(String regex, int limit)
What would the values of the array elements be?
String name = “Ana, Maria, Carla, Vina, Glen”
String breakName[] = name.split(“ “)
breakName[0] = Ana
breakName[1] = Maria
breakName[2] = Carla
breakName [3] = Vina
breakName[4] = Glen
Split the following into the month, day, and year.
May 11, 2022
date = “May 11, 2002”;
date1 = date.split(“,”);
// date1[0] = May 11,
// date1[1] = 2002
date2 = date1[0].split(“ “);
// date2[0] = May
// date2[1] = 11
// date1[1] = 2002
String sentences = “Java Programming is fun and easy”;
String splitSentence[] = sentence.split(“ “);
System.out.println(“Sentence: “ + sentence);
System.out.println(“After Split”);
for (int x = 0; x<splitSentence.length; x++) {
System.out.println(“splitSentence[” + x + “]” + splitSentence[x]);
}
splitSentence[0] Java
splitSentence[1] Programming
splitSentence[2] is
splitSentence[3] fun
splitSentence[4] and
splitSentence[5] easy