'How to unstructure a text string that has characters and convert it to an object
I have a text string that I receive from PL/SQL. I need to convert each object that is separated by a ';'. This is my string enter image description here I need to get that information and create the objects that come in that string.
//all objects come in a single text string
first object is 830114921-1<,>TIGO<,>13<,>RECARGA CELULAR TIGO;
second object is 800153993-7<,>PROTOCOLO CLARO<,>426<,>PAQUECLARO 24HRS;
third object is 800153993-7<,>PROTOCOLO CLARO<,>7<,>PINES 2000;
fourth object is 900102005-1<,>GLOBAL TV TELECOMUNICACIONES<,>92<,>JOHATRUJILLO;
Example:
Class MyClass{
Integer number;
String text;
Integer number2;
String text;
}
List<MyClass> myList = new ArrayList<>();
myClass.SetNumber(830114921-1);
myClass.SetText("TIGO");
myClass.SetNumber2(13);
myClass.SetText2("RECARGA CELULAR TIGO");
myList.add(myClass);
//I can split the 4 objects but if more objects come in the text string, can //no longer be.
String str = "830114921-1<,>TIGO<,>13<,>RECARGA CELULAR TIGO;"
+ " 800153993-7<,>PROTOCOLO CLARO<,>426<,>PAQUECLARO 24HRS; "
+ "800153993-7<,>PROTOCOLO CLARO<,>7<,>PINES 2000; "
+ "900102005-1<,>GLOBAL TV TELECOMUNICACIONES<,>92<,>JOHATRUJILLO;";
String[] arrOfStr = str.split(";", 4);
List<String> list = new ArrayList<>();
for (String a : arrOfStr) {
list.add(a);
}
for (String data : list) {
String result2 = data.replaceAll("\\<,>", " ");
System.out.println(result2);
}
Solution 1:[1]
The data is split with <,> and ; so you should easily be able to split the string and collect the data. Java has a String.split() method. First split the string object that contains all the data into an array of strings by splitting on ; and then loop over these and split them on <,>.
At this point you should be able to set your attributes according to the order of the data in the string (assuming these are ordered properly).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PuzzledScratch |
