Different Ways to Generate String by using Characters and Numbers in Java Last Updated : 05 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Given a number num and String str, the task is to generate the new String by extracting the character from the string by using the index value of numbers. Examples: Input: str = “GeeksforGeeks” num = 858 Output: GfG Explanation: The 8th, 5th, and 8th position of the characters are extracting from the given string and generate a new string. Input: str = “Java” num = 12 Output: The 1st and 2nd position of the characters are extracting from the given string and generate a new string. Method 1: Using Two Traversals Get the string and number to generate a new string.Initialize a variable that stores the final answer.Converting the given number into the string.Traverse the loop from start to end.Getting the last digit of the number and add kth position character into the variable answer.Traverse the loop in the reverse order and adding the jth position character into the variable finalAnswer.Now, print the result. Java // Java program to generate String by using // Strings and numbers class GFG { // Function to generate the new String public static String generateNewString(String str, int num) { // Initialize a variable that // stores the final answer. String finalAnswer = ""; String answer = ""; // Converting the given numbers // into string String index = String.valueOf(num); // Traverse the loop from start to end for (int i = 0; i < index.length(); i++) { // Getting last digit of numbers int k = num % 10; // Adding kth position character // into the variable answer answer = answer + str.charAt(k); num = num / 10; } // Traverse the loop in reverse order for (int j = answer.length() - 1; j >= 0; j--) { // Adding jth position character // into the variable finalAnswer finalAnswer = finalAnswer + answer.charAt(j); } // Return the finalAnswer return finalAnswer; } // Driver Code public static void main(String args[]) { // Given String str String str = "GeeksforGeeks"; // Given Number num int num = 858; // Printing the result System.out.println(generateNewString(str, num)); } } OutputGfG Method 2: Using One Traversal Get the string and number to generate a new string.Initialize a variable that stores the result.Converting the given number into the string.Traverse the loop from start to end.Get the right index and adding kth position character into the result.Now, print the result. Java // Java program to generate String by using // Strings and numbers class GFG { // Function to generate the new String public static String generateNewString(String str, int num) { // Initialize a variable that // stores the result. String result = ""; // Converting the given numbers // into the string String index = String.valueOf(num); // Traverse the loop from start to end for (int i = 0; i < index.length(); i++) { // Getting the right index int k = index.charAt(i) - 48; // Adding kth position character // into the result result = result + str.charAt(k); } // Return result return result; } // Driver Code public static void main(String args[]) { // Given String str String str = "GeeksforGeeks"; // Given Number num int num = 858; // Printing the result System.out.println(generateNewString(str, num)); } } OutputGfG Time Complexity: O(N) Comment More infoAdvertise with us Next Article Different Ways to Generate String by using Characters and Numbers in Java P prasanthcrmp Follow Improve Article Tags : Misc Strings Java DSA strings +1 More Practice Tags : JavaMiscStringsStrings Similar Reads Difference between String and Character array in Java Unlike C/C++ Character arrays and Strings are two different things in Java. Both Character Arrays and Strings are a collection of characters but are different in terms of properties. Differences between Strings and Character Arrays:PropertyStringCharacter ArrayDefinitionA sequence of characters is r 3 min read Convert a String to a List of Characters in Java In Java, to convert a string into a list of characters, we can use several methods depending on the requirements. In this article, we will learn how to convert a string to a list of characters in Java.Example:In this example, we will use the toCharArray() method to convert a String into a character 3 min read Different Ways to Remove all the Digits from String in Java Given alphanumeric string str, the task is to write a Java program to remove all the digit from this string and prints the modified string. Examples: Input: str = âGeeksForGeeks123âOutput: GeeksForGeeksExplanation: The given string contains digits 1, 2, and 3. We remove all the digit and prints the 3 min read Java Convert int to String - Different Ways of Conversion Converting an int to a String is an important type conversion. Many operations can be performed over a string, while we are limited when it comes to integers. We have a wide varied list of in-built methods in the String class that help us perform hassle-free operations. Suppose we are required to co 9 min read Java Program to Get a Character from a String Given a String str, the task is to get a specific character from that String at a specific index. Examples:Input: str = "Geeks", index = 2Output: eInput: str = "GeeksForGeeks", index = 5Output: F Below are various ways to do so: Using String.charAt() method: Get the string and the indexGet the speci 5 min read How to Convert Character Array to String in Java? Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character "\0". A character array can be converted to a string and vice versa. In this article, we will discuss how to convert a character array to a string 4 min read Convert List of Characters to String in Java Given a list of characters. In this article, we will write a Java program to convert the given list to a string. Example of List-to-String ConversionInput : list = {'g', 'e', 'e', 'k', 's'} Output : "geeks" Input : list = {'a', 'b', 'c'} Output : "abc" Strings - Strings in Java are objects that are 4 min read Different ways for String to Integer Conversions in Java Given a String in Java, the task is to convert this String into Integer. Examples: Input: str = "1234" Output: 1234 Input: str = "456" Output: 456 Convert using Integer.parseInt(String) The Integer class has a static method that returns an integer object representing the specified String parameter. 2 min read Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings) Set 1 (Random Numbers, Arrays and Matrices) Generating Random Characters CPP // A C++ Program to generate test cases for // random characters #include<bits/stdc++.h> using namespace std; // Define the number of runs for the test data // generated #define RUN 5 // Define the range of the test d 11 min read Swapping Characters of a String in Java As we know that Object of String in Java are immutable (i.e. we cannot perform any changes once its created). To do modifications on string stored in a String object, we copy it to a character array, StringBuffer, etc and do modifications on the copy object.In this article we would go through some m 3 min read Like