Which of these methods of class string is used to extract all the characters from a string object?

Which of these methods of class string is used to extract all the characters from a string object?
Which of these methods of class string is used to extract all the characters from a string object?

McqMate.com is an educational platform, Which is developed BY STUDENTS, FOR STUDENTS, The only objective of our platform is to assist fellow students in preparing for exams and in their Studies throughout their Academic career.

what we offer ?

» We provide you study material i.e. PDF's for offline use.» We take free online Practice/Mock test for exam preparation.» Each MCQ is open for further discussion on discussion page.

» All the services offered by McqMate are free.

Email:


Popular

Quick Links

Here you can find Strings Questions and Answers.

Why Strings Questions and Answers Required?

In this Strings Questions and Answers section you can learn and practice Strings Questions and Answers to improve your skills in order to face technical inerview conducted by organisations. By Practicing these interview questions, you can easily crack any Exams interview.

Where can I get Strings Questions and Answers?

AllIndiaExams provides you lots Strings Questions and Answers with proper explanation. Fully solved examples with detailed answer description. All students, freshers can download Strings Questions and Answers as PDF files and eBooks.

How to solve these Strings Questions and Answers?

You no need to worry, we have given lots of Strings Questions and Answers and also we have provided lots of FAQ's to quickly answer the questions in the Competitive Exams interview.

Strings Questions and Answers

1. Which of these class is superclass of String and StringBuffer class?

Strings Questions and Answers

2. Which of these operators can be used to concatenate two or more String objects?

Strings Questions and Answers

3. Which of these method of class String is used to obtain length of String object?

Strings Questions and Answers

4. Which of these method of class String is used to extract a single character from a String object?

Strings Questions and Answers

5. Which of these constructors is used to create an empty String object?

Strings Questions and Answers

6. Which of these is an oncorrect statement?

Strings Questions and Answers

7. Which of these method of class String is used to compare two String objects for their equality?

Strings Questions and Answers

8. Which of these methods is used to compare a specific region inside a string with another specific region in another string?

Strings Questions and Answers

9. Which of these method of class String is used to check weather a given object starts with a particular string literal?

Strings Questions and Answers

10. What is the value returned by function compare to() if the invoking string is less than the string compared?


The String class provides a number of ways in which characters can be extracted from a String object.

In this post, we will see several character extraction methods. Although the characters that comprise a string within a String object cannot be indexed as if they were a character array, many of the String methods employ an index (or offset) into the string for their operation. Like arrays, the string indexes begin at zero.

  • charAt()
  • getChars()
  • getBytes()
  • toCharArray()

To extract a single character from a String, you can refer directly to an individual character via the charAt( ) method.

Example 1: Returns the char value at the specified index of this string. The first char value is at index 0.

String str = "Welcome to string handling guide"; char ch1 = str.charAt(0); char ch2 = str.charAt(5); char ch3 = str.charAt(11); char ch4 = str.charAt(20); System.out.println("Character at 0 index is: " + ch1); System.out.println("Character at 5th index is: " + ch2); System.out.println("Character at 11th index is: " + ch3); System.out.println("Character at 20th index is: " + ch4);

Character at 0 index is: W Character at 5th index is: m Character at 11th index is: s Character at 20th index is: n

Example 2: Throws IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.

String str = "Java Guides"; char ch1 = str.charAt(str.length() + 1); System.out.println("character :: " + ch1);

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12 at java.lang.String.charAt(String.java:658) at com.javaguides.strings.methods.ChatAtExample.charAtExample2(ChatAtExample.java:26) at com.javaguides.strings.methods.ChatAtExample.main(ChatAtExample.java:6)

Example 3: How to get the first and last character of the string

String str = "Java Guides"; int strLength = str.length(); // Fetching first character System.out.println("Character at 0 index is: " + str.charAt(0)); // The last Character is present at the string length-1 index System.out.println("Character at last index is: " + str.charAt(strLength - 1));

Character at 0 index is: J Character at last index is: s

If you need to extract more than one character at a time, you can use the getChars( ) method.

It has this general form:

void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)

Here, sourceStart specifies the index of the beginning of the substring, and sourceEnd specifies an index that is one past the end of the desired substring. Thus, the substring contains the characters from sourceStart through sourceEnd–1.

The array that will receive the characters is specified by target. The index within the target at which the substring will be copied is passed in targetStart. Care must be taken to assure that the target array is large enough to hold the number of characters in the specified substring.

The following program demonstrates the usage of getChars() method:

class getCharsDemo { public static void main(String args[]) { String s = "This is a demo of the getChars method."; int start = 10; int end = 14; char buf[] = new char[end - start]; s.getChars(start, end, buf, 0); System.out.println(buf); } }

Here is the output of this program:

demo

There are four versions of getBytes() methods. There is an alternative to getChars( ) that stores the characters in an array of bytes.

byte[] getBytes() - Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

byte[] getBytes(Charset charset) - Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.

void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) - Deprecated.

byte[] getBytes(String charsetName) - Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

Let's write an example to demonstrate all the getBytes() methods.
public class GetBytesExamples { public static void main(String[] args) throws UnsupportedEncodingException { String str = "javaguides"; // Encodes this String into a sequence of bytes using the platform's // default charset, storing the result into a new byte array. byte[] bs = str.getBytes(); for (byte b: bs) { System.out.println(b); } // Encodes this String into a sequence of bytes using the given charset, // storing the result into a new byte array. byte[] bs1 = str.getBytes(Charset.forName("UTF-8")); for (byte b: bs1) { System.out.println(b); } // Encodes this String into a sequence of bytes using the given charset, // storing the result into a new byte array. byte[] bs2 = str.getBytes("UTF-8"); for (byte b: bs2) { System.out.println(b); } byte[] dest = new byte[str.length()]; str.getBytes(0, str.length(), dest, 0); for (byte b: dest) { System.out.println(b); } } }

This method converts this string to a new character array.

Example: This is a complete example to demonstrate the usage of the toCharArray() method.

public class ToCharArrayExample { public static void main(String[] args) { String str = "javaguides"; char[] characters = str.toCharArray(); for (char c : characters) { System.out.println(c); } } }

j a v a g u i d e s

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course