Which method of class StringBuffer is used to extract a substring from a String object?
This is the java programming questions and answers section on "Java Strings" with an explanation for various interview, competitive examination and entrance test. Solved examples with detailed answer description, explanation are given and it would be easy to understand. Show
Java Strings Questions
21. Which of the following statement is correct?
Answer & ExplanationAnswer: Option A Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called. WorkspaceReport Error22. Which of these method of class StringBuffer is used to extract a substring from a String object?
WorkspaceReport Error
23. What will s2 contain after following lines of code? StringBuffer s1 = "one; StringBuffer s2 = s1.append("two)
Answer & ExplanationAnswer: Option C Explanation: Two strings can be concatenated by using append() method. WorkspaceReport Error24. Which of these method of class StringBuffer is used to reverse sequence of characters?
Answer & ExplanationAnswer: Option A Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called. WorkspaceReport Error25. Which of these method of class StringBuffer is used to get the length of sequence of characters?
Answer & ExplanationAnswer: Option A Explanation: length()- returns the length of String the StringBuffer would create whereas capacity() returns total number of characters that can be supported before it is grown. WorkspaceReport ErrorMock Tests & Online QuizzesA part of String is called substring. In other words, substring is a subset of another String. Java String class provides the built-in substring() method that extract a substring from the given string by using the index values passed as an argument. In case of substring() method startIndex is inclusive and endIndex is exclusive. Suppose the string is "computer", then the substring will be com, compu, ter, etc. Note: Index starts from 0.You can get substring from the given String object by one of the two methods:
In case of String:
Let's understand the startIndex and endIndex by the code given below. In the above substring, 0 points the first letter and 2 points the second letter i.e., e (because end index is exclusive). Example of Java substring() methodTestSubstring.java Output: Original String: SachinTendulkar Substring starting from index 6: Tendulkar Substring starting from index 0 to 6: Sachin The above Java programs, demonstrates variants of the substring() method of String class. The startindex is inclusive and endindex is exclusive. Using String.split() method:The split() method of String class can be used to extract a substring from a sentence. It accepts arguments in the form of a regular expression. TestSubstring2.java Output: [Hello, My name is Sachin] In the above program, we have used the split() method. It accepts an argument \\. that checks a in the sentence and splits the string into another string. It is stored in an array of String objects sentences. Which of these method of class StringBuffer is used?Explanation: append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
Which one of the following method is used to make substring?Java String substring() The Java String class substring() method returns a part of the string. We pass beginIndex and endIndex number position in the Java substring method where beginIndex is inclusive, and endIndex is exclusive.
What is StringBuffer method?A string buffer is like a String , but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. String buffers are safe for use by multiple threads.
Which of the method of class String is used to obtain a length of string object?Which of this method of class String is used to obtain a length of String object? Explanation: Method length() of string class is used to get the length of the object which invoked method length().
|