So I have, for example, a string such as this C3H20IO
What I wanna do is split this string so I get the following:
Array1 = {C,H,I,O}
Array2 = {3,20,1,1}
The 1
as the third element of the Array2
is indicative of the monoatomic nature of the I
element. Same for O
. That is actually the part I am struggling with.
This is a chemical equation, so I need to separate the elements according to their names and the amount of atoms there are etc.
split() The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings. We can also pass a limit to the number of elements in the returned array.
Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.
I did this as following
ArrayList<Integer> integerCharacters = new ArrayList();
ArrayList<String> stringCharacters = new ArrayList<>();
String value = "C3H20IO"; //Your value
String[] strSplitted = value.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"); //Split numeric and strings
for(int i=0; i<strSplitted.length; i++){
if (Character.isLetter(strSplitted[i].charAt(0))){
stringCharacters.add(strSplitted[i]); //If string then add to strings array
}
else{
integerCharacters.add(Integer.parseInt(strSplitted[i])); //else add to integer array
}
}
You could try this approach:
String formula = "C3H20IO";
//insert "1" in atom-atom boundry
formula = formula.replaceAll("(?<=[A-Z])(?=[A-Z])|(?<=[a-z])(?=[A-Z])|(?<=\\D)$", "1");
//split at letter-digit or digit-letter boundry
String regex = "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)";
String[] atoms = formula.split(regex);
Output:
atoms: [C, 3, H, 20, I, 1, O, 1]
Now all even even indices (0, 2, 4...) are atoms and odd ones are the associated number:
String[] a = new String[ atoms.length/2 ];
int[] n = new int[ atoms.length/2 ];
for(int i = 0 ; i < a.length ; i++) {
a[i] = atoms[i*2];
n[i] = Integer.parseInt(atoms[i*2+1]);
}
Output:
a: [C, H, I, O]
n: [3, 20, 1, 1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With