I have a string, ie 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17. How do I get each value and convert it into an array? [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]. I can't find any suggestions about this method. Can help? I did try using regex, but it just simply remove ',' and make the string into one long sentence with indistinguishable value. Is it ideal to get value before and after ',' with regex and put it into []?
You could use following solution
String dummy = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] dummyArr = dummy.split(",");
Try this to convert string to an array of Integer.
String baseString = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] baseArray = baseString.split(",");
int[] myArray = new int[baseArray.length];
for(int i = 0; i < baseArray.length; i++) {
myArray[i] = Integer.parseInt(baseArray[i]);
}
Java provides method Split with regex argument to manipulate strings.
Follow this example:
String strNumbers= "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] strNumbersArr= strNumbers.split(",");
You can convert an array of string in array of integer with Streams
int[] numbersArr = Arrays.stream(strNumbersArr).mapToInt(Integer::parseInt).toArray();
Use String.split() and you will get your desired array.
String s1="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] mumbers=s1.split(","); //splits the string based on comma
for(String ss:numbers){
System.out.println(ss);
}
See the working Example
String csv = "Apple, Google, Samsung";
String[] elements = csv.split(",");
List<String> fixedLenghtList = Arrays.asList(elements);
ArrayList<String> listOfString = new ArrayList<String>(fixedLenghtList);
//ouput
[Apple, Google, Samsung]
if you want an int array
String s = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] split = s.split(",");
int[] result = Arrays.stream(split).mapToInt(Integer::parseInt).toArray();
how to remove "," in below string specified at s[0,2] location(818001,818002)(151515,151515) and store again to string []
String[] s = {"818001,818002","121212","151515,151515"};
You should explain more detailed your problem.
I think what you want is this:
s = '(818001,818002)(151515,151515)' //the string you have.
s = s.replace(')(',',').replace('(','').replace(')','');// output "818001,818002,151515,151515"
final_string = s.split(','); //output (4) ["818001", "818002", "151515", "151515"]
If you need to remove a comma (or any other char for that matter) in your String array indices the simplest is to iterate through the array and use String.replace():
String[] s = {"818001,818002","121212","151515,151515"};
for(int i=0; i<s.length; i++) {
s[i] = s[i].replace(",", "");
}
System.out.print(Arrays.toString(s)); // [818001818002, 121212, 151515151515]
I need your help. I have a string with value "1,2,3,4,5,6,7,8,9,10".
What I want to do is take once first value ("1") only (substring (0, 1) for example) and then do a loop with the rest of values except the first value that I already take.
Maybe I have to create another String variable and set the values without first value to the second String variable and then create a loop? How to do that?
The easiest way would probably be to use String#split(String):
String str = "1,2,3,4,5,6,7,8,9,10";
String[] parts = str.split(",");
// Save the first part
String firstPart = parts[0];
// Iterate over the others:
for (int i = 1; i < parts.length; ++i) {
System.out.println (parts[i]); // Or do something useful with it
}
You can use split function.
String numbers = "1,2,3,4,5,6,7,8,9,10"; //Here your String
String[] array = numbers.split(","); //Here you divide the String taking as reference the ,
String number = array[0] //You will get the number 1
If you want to take the rest of the elements:
for(i = 1; i < array.length; i++)
System.out.println(array[i]);
I expect it will be helpful for you!
How do i read a file and determine the # of array elements without having to look at the text file itself?
String temp = fileScan.toString();
String[] tokens = temp.split("[\n]+");
// numArrayElements = ?
Use the length property of the array:
int numArrayElements = tokens.length;
The proper expression is tokens.length. So, you can assign numArrayElements like this:
int numArrayElements = tokens.length;
This counts the number of elements in the tokens array. You can count the number of elements in any array in the same way.
I have got a Java String as follows:
C|51199120|36937872|14261248|0.73|I|102398308|6240560|96157748|0.07|J|90598564|1920184|8867 8380|0.0
I want split this using regex as String arrays:
Array1 = C,51199120,36937872,14261248,0.73
Array2 =I,102398308,6240560,96157748,0.07
Array3 =J,90598564,1920184,88678380,0.03
Can Anybody help with Java code?
I don't think it's that simple. You have two things you need to do:
Break up the input string when you encounter a letter
Break up each substring by the pipe
I'm no regex expert, but I don't think it can be a single pattern. You need two and a loop over the substrings.
You can easily split your string on subcomponents using String.split("\\|"), but regexes won't help you to group them up in different arrays, nor will it help you to convert substrings to appropriate type. You'll need a separate logic for that.
Use String.split() method.
String []ar=str.split("(?=([A-Z]))");
for(String s:ar)
System.out.println(s.replace("|",","));
Simpler to just split then loop.
More or less:
String input = ...
String[] splitted = input.split("|");
List<String[]> resultArrays = new ArrayList<String[]>();
String[] currentArray = null;
for (int i = 0; i < splitted.length; i++) {
if (i % 5 == 0) {
currentArray = new String[5];
resultArrays.put(currentArray);
}
currentArray[i%5] = splitted[i];
}