Which method of String class is most appropriate for extracting all the characters of String into an array?
This article discusses different ways to reverse a string in Java with examples. Show
Examples: Prerequisite: String vs StringBuilder vs StringBuffer in JavaFollowing are some interesting facts about String and StringBuilder classes :
Implementation: Javaimport java.io.*; import java.util.Scanner; class GFG { public static void main (String[] args) { String str= "Geeks", nstr=""; char ch; System.out.print("Original word: "); System.out.println("Geeks"); for (int i=0; i { ch= str.charAt(i); nstr= ch+nstr; } System.out.println("Reversed word: "+ nstr); } } Output Converting String into Bytes: getBytes() method is used to convert the input string into bytes[].Method: 1. Create a temporary byte[] of length equal to the length of the input string. 2. Store the bytes (which we get by using getBytes() method) in reverse order into the temporary byte[] . 3. Create a new String abject using byte[] to store result.Implementation: Javaimport java.lang.*; import java.io.*; import java.util.*; class ReverseString { public static void main(String[] args) { String input = "GeeksforGeeks"; byte[] strAsByteArray = input.getBytes(); byte[] result = new byte[strAsByteArray.length]; for (int i = 0; i < strAsByteArray.length; i++) result[i] = strAsByteArray[strAsByteArray.length - i - 1]; System.out.println(new String(result)); } } Using built in reverse() method of the StringBuilder class:String class does not have reverse() method, we need to convert the input string to StringBuilder, which is achieved by using the append method of StringBuilder. After that, print out the characters of the reversed string by scanning from the first till the last index. Implementation: Javaimport java.lang.*; import java.io.*; import java.util.*; class ReverseString { public static void main(String[] args) { String input = "Geeks for Geeks"; StringBuilder input1 = new StringBuilder(); input1.append(input); input1.reverse(); System.out.println(input1); } } Converting String to character array: The user input the string to be reversed.Method: 1. First, convert String to character array by using the built in Java String class method toCharArray(). 2. Then, scan the string from end to start, and print the character one by one.Implementation: Javaimport java.lang.*; import java.io.*; import java.util.*; class ReverseString { public static void main(String[] args) { String input = "GeeksForGeeks"; char[] try1 = input.toCharArray(); for (int i = try1.length - 1; i >= 0; i--) System.out.print(try1[i]); } }
Implementation: Javaimport java.lang.*; import java.io.*; import java.util.*; class ReverseString { public static void main(String[] args) { String input = "Geeks For Geeks"; char[] temparray = input.toCharArray(); int left, right = 0; right = temparray.length - 1; for (left = 0; left < right; left++, right--) { char temp = temparray[left]; temparray[left] = temparray[right]; temparray[right] = temp; } for (char c : temparray) System.out.print(c); System.out.println(); } }
Implementation: Javaimport java.lang.*; import java.io.*; import java.util.*; class ReverseString { public static void main(String[] args) { String input = "Geeks For Geeks"; char[] hello = input.toCharArray(); List for (char c : hello) trial1.add(c); Collections.reverse(trial1); ListIterator li = trial1.listIterator(); while (li.hasNext()) System.out.print(li.next()); } } Using StringBuffer:String class does not have reverse() method, we need to convert the input string to StringBuffer, which is achieved by using the reverse method of StringBuffer. Implementation: Javaimport java.lang.*; import java.io.*; import java.util.*; public class Test { public static void main(String[] args) { String str = "Geeks"; StringBuffer sbr = new StringBuffer(str); sbr.reverse(); System.out.println(sbr); } }
Javaimport java.io.*; import java.util.Scanner; class GFG { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); String Str = scanner.nextLine(); char[] arr = Str.toCharArray(); String rev = ""; for(int i = Str.length() - 1; i >= 0; i--) { rev = rev + Str.charAt(i); } System.out.println(rev); } } In the above code, we are essentially reading a String from the user before starting an iteration loop to create a new, inverted String. The “charAt” function of the String class is used to retrieve each character of the original String individually from the end, and the “+” operator is used to concatenate them into a new String.
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks Which of the following method of String class is most appropriate for extracting all the characters of String into an array?Explanation: Because we are performing operation on reference variable which is null. 5. Which of these methods can be used to convert all characters in a String into a character array? Explanation: charAt() return one character only not array of character.
Which methods are used to extract the characters from String?Below are various ways to do so:. Using String. charAt() method: Get the string and the index. ... . Using String. toCharArray() method: Get the string and the index. ... . Using Java 8 Streams: Get the string and the index. Convert String into IntStream using String. ... . Using String. codePointAt() method: ... . Using String. getChars() method:. Which of these methods of class String is used to extract all the characters from a String object * A charAt () b remove () C charAt () d replace ()?Explanation: Replace() replaces all instances of a character with a new character while Remove extracts characters from the string.
Which method of String class is used to extract a particular character in the String?charAt() is the method of class String is used to extract a single character from a String Object. Hence, option(c) is the correct answer.
|