Strings – Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are also a type of exceptional array that holds characters, therefore, strings are immutable as well.
Integer Array – An array is a combination of the same type of variables. Likewise, a collection of integers (int) referred to by a common name is an Integer Array.
Problem Statement – Given a string, the task is to convert that string into an integer array.
Let us first go through few examples to make it more clear.
Examples:
Input : String : "1 23 456 7890"
Output : Integer array : [1 23 456 7890]
Input : String : "[1,2,356,678,3378]"
Output : Integer array : [1, 2, 356, 678, 3378]
There are numerous approaches to do the same; a few of them are listed below.
Methods:
- Using string.replaceAll() method
- Using string.split() method
Method 1 – Using string.replaceAll() method
The string.replaceAll() method takes two arguments, a regex, and the replacement values. This method will replace the given regex with the given replacement value, and after that, the split() method is used to split the string.
Java
import java.io.*;
import java.util.Arrays;
public class GFG {
public static void main(String[] args)
{
String str = "[1,2,356,678,3378]" ;
String[] string = str.replaceAll( "\\[" , "" )
.replaceAll( "]" , "" )
.split( "," );
int [] arr = new int [string.length];
for ( int i = 0 ; i < string.length; i++) {
arr[i] = Integer.valueOf(string[i]);
}
System.out.print( "String : " + str);
System.out.print( "\nInteger array : "
+ Arrays.toString(arr));
}
}
|
Output
String : [1,2,356,678,3378]
Integer array : [1, 2, 356, 678, 3378]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 – Using string.split() method
The string.split() method is used to split the string into various sub-strings. Then, those sub-strings are converted to an integer using the Integer.parseInt() method and store that value integer value to the Integer array.
Java
import java.io.*;
public class GFG {
static int [] method(String str)
{
String[] splitArray = str.split( " " );
int [] array = new int [splitArray.length];
for ( int i = 0 ; i < splitArray.length; i++) {
array[i] = Integer.parseInt(splitArray[i]);
}
return array;
}
public static void main(String[] args)
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
String str = "1 23 456 7890" ;
int i;
int [] array = method(str);
System.out.print( "\nString : " + str);
System.out.print( "\nInteger array : [" );
for (i = 0 ; i < array.length; i++) {
System.out.print(array[i] + " " );
}
System.out.print( "]" );
}
}
|
Output
String : 1 23 456 7890
Integer array : [1 23 456 7890 ]
Time Complexity: O(n)
Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Feb, 2023
Like Article
Save Article