Podcast
Questions and Answers
Which method is used to convert a String to a float?
Which method is used to convert a String to a float?
What is the result of explicitly casting the double 5.99 to an int?
What is the result of explicitly casting the double 5.99 to an int?
How do you convert an int to a char?
How do you convert an int to a char?
What is the output of the following code: String str = 'HELLO'; String lower = str.toLowerCase(); System.out.println(lower);
What is the output of the following code: String str = 'HELLO'; String lower = str.toLowerCase(); System.out.println(lower);
Signup and view all the answers
Which statement correctly converts a long to a String?
Which statement correctly converts a long to a String?
Signup and view all the answers
What will be the output of 'char c = str.charAt(0);' if str = 'World'?
What will be the output of 'char c = str.charAt(0);' if str = 'World'?
Signup and view all the answers
Which method is used to remove leading and trailing whitespace from a string?
Which method is used to remove leading and trailing whitespace from a string?
Signup and view all the answers
Which of the following statements correctly describes the split(String regex) method?
Which of the following statements correctly describes the split(String regex) method?
Signup and view all the answers
Which method should be used to check if a String can be converted to a boolean?
Which method should be used to check if a String can be converted to a boolean?
Signup and view all the answers
What does the startsWith(String prefix) method return when checking if the string 'Java Programming' starts with 'Java'?
What does the startsWith(String prefix) method return when checking if the string 'Java Programming' starts with 'Java'?
Signup and view all the answers
When converting a float to an int, what type of conversion is required?
When converting a float to an int, what type of conversion is required?
Signup and view all the answers
Which method is NOT used for String to Character conversion?
Which method is NOT used for String to Character conversion?
Signup and view all the answers
What does the endsWith(String suffix) method return when checking if 'Hello, World!' ends with 'World!'?
What does the endsWith(String suffix) method return when checking if 'Hello, World!' ends with 'World!'?
Signup and view all the answers
Which output is generated by the following code: String str = 'hello'; String newStr = str.replace('l', 'p'); System.out.println(newStr);
Which output is generated by the following code: String str = 'hello'; String newStr = str.replace('l', 'p'); System.out.println(newStr);
Signup and view all the answers
What is returned by the isEmpty() method when applied to a newly instantiated string object?
What is returned by the isEmpty() method when applied to a newly instantiated string object?
Signup and view all the answers
What will be the output of comparing the strings 'apple' and 'banana' using the compareTo method?
What will be the output of comparing the strings 'apple' and 'banana' using the compareTo method?
Signup and view all the answers
How do you convert a string representing a number to an integer?
How do you convert a string representing a number to an integer?
Signup and view all the answers
Which method would you use to join multiple strings with a delimiter?
Which method would you use to join multiple strings with a delimiter?
Signup and view all the answers
What does the toCharArray() method do to a string?
What does the toCharArray() method do to a string?
Signup and view all the answers
When using compareToIgnoreCase on 'apple' and 'Banana', what will the result be?
When using compareToIgnoreCase on 'apple' and 'Banana', what will the result be?
Signup and view all the answers
What type of casting is performed when converting a string to an integer using Integer.valueOf()?
What type of casting is performed when converting a string to an integer using Integer.valueOf()?
Signup and view all the answers
Which method can be used to convert an int to a string?
Which method can be used to convert an int to a string?
Signup and view all the answers
What is the correct way to convert a string '123.45' to a double?
What is the correct way to convert a string '123.45' to a double?
Signup and view all the answers
Study Notes
Java String Methods
-
length()
: Returns the length of the string.- Example:
"Hello, World!"
.length() returns 13
- Example:
-
charAt(int index)
: Returns the character at the specified index.- Example:
"Hello"
.charAt(1) returns 'e'
- Example:
-
substring(int beginIndex)
: Returns a substring starting from the specified index.- Example:
"Hello, World!"
.substring(7) returns "World!"
- Example:
-
substring(int beginIndex, int endIndex)
: Returns a substring frombeginIndex
toendIndex
- 1.- Example:
"Hello, World!"
.substring(0, 5) returns "Hello"
- Example:
-
contains(CharSequence sequence)
: Checks if the string contains the specified sequence of characters.- Example:
"Hello, World!"
.contains("World") returns true
- Example:
-
equals(Object another)
: Compares the string to the specified object for equality (case sensitive).- Example:
"Hello"
.equals("Hello") returns true
- Example:
-
equalsIgnoreCase(String another)
: Compares two strings, ignoring case differences.- Example:
"HELLO"
.equalsIgnoreCase("hello") returns true
- Example:
-
indexOf(String str)
: Returns the index of the first occurrence of the substring.- Example:
"Hello, World!"
.indexOf("World") returns 7
- Example:
-
indexOf(String str, int fromIndex)
: Returns the index of the first occurrence of the substring starting from the specified index.- Example:
"Hello, World! World!"
.indexOf("World", 8) returns 14
- Example:
-
lastIndexOf(String str)
: Returns the index of the last occurrence of the substring.- Example:
"Hello, World! World!"
.lastIndexOf("World") returns 14
- Example:
-
toUpperCase()
: Converts all characters in the string to uppercase.- Example:
"hello"
.toUpperCase() returns "HELLO"
- Example:
-
toLowerCase()
: Converts all characters in the string to lowercase.- Example:
"HELLO"
.toLowerCase() returns "hello"
- Example:
-
trim()
: Removes leading and trailing whitespace.- Example:
" Hello, World! "
.trim() returns "Hello, World!"
- Example:
-
replace(char oldChar, char newChar)
: Replaces all occurrences of a specified character with a new character.- Example:
"hello"
.replace('l', 'p') returns "heppo"
- Example:
-
replace(CharSequence target, CharSequence replacement)
: Replaces all occurrences of a substring with another substring.- Example:
"Hello, World!"
.replace("World", "Java") returns "Hello, Java!"
- Example:
-
split(String regex)
: Splits the string into an array based on the given regular expression.- Example:
"apple,orange,banana"
.split(",") returns an array containing "apple", "orange", and "banana"
- Example:
-
split(String regex, int limit)
: Splits the string into an array with a maximum number of substrings.- Example:
"apple,orange,banana"
.split(",",2) returns an array containing "apple" and "orange,banana".
- Example:
-
startsWith(String prefix)
: Checks if the string starts with the specified prefix.- Example:
"Hello, World!"
.startsWith("Hello") returns true
- Example:
-
endsWith(String suffix)
: Checks if the string ends with the specified suffix.- Example:
"Hello, World!"
.endsWith("World!") returns true
- Example:
-
isEmpty()
: Checks if the string is empty (length is 0).- Example:
"".isEmpty()
returns true
- Example:
-
join(CharSequence delimiter, CharSequence... elements)
: Joins multiple strings with the specified delimiter.- Example:
String.join("-", "2024", "10", "20")
returns "2024-10-20"
- Example:
-
compareTo(String anotherString)
: Compares two strings lexicographically.- Example:
"apple"
.compareTo("banana") returns a negative value
- Example:
-
compareToIgnoreCase(String anotherString)
: Compares two strings lexicographically, ignoring case differences.- Example:
"apple"
.compareToIgnoreCase("Banana") returns a negative value (because apple < Banana)
- Example:
Java Type Casting
-
String to Integer (int):
-
Integer.parseInt(str)
-
Integer.valueOf(str)
-
-
Integer to String:
-
String.valueOf(int)
-
Integer.toString(int)
-
-
String to double/float/long:
-
Double.parseDouble(str)
-
Float.parseFloat(str)
-
Long.parseLong(str)
-
-
Other Type Conversions: String to boolean, boolean to string, char to int, int to char, Other conversions are implicit or explicit, depending on the context and data types' sizes.
-
Using Concatenation: You can convert an int to a String by concatenating it with an empty string (e.g.,
123 + ""
).
Additional Notes
- Methods like
toCharArray
,valueOf
handle various data types, converting them to Strings. - Java has implicit and explicit type conversion rules; implicitly, in some cases, conversion might occur based on data type compatibility. Explicitly, conversion might be required through casting to change data type.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge on Java String methods with this quiz. You'll explore various methods such as length, charAt, and substring. Perfect for beginners and those brushing up on their Java programming skills.