I want 4 strings, and each one needs to be a certain section of an "input" string.
input:
"AP 3A:23:03:DA:30:50 name 2427/20/gn(27dBm) -91 -104 13"
output:
string1 = "name"
string2 = "2427"
string3 = "20"
string4 = "-91"
I having trouble finding a consistent way to do this, because potential input strings could look like this:
AP F8:CA:59:6D:29:56 AWI-500544 2412/20/gn(27dBm) -33 -103 70
or this:
AP 30:23:03:DA:30:50 Device/Mesh2566 2427/20/gn(27dBm) -89 -104 15
string1 could potentially contain a "/" or pattern of numbers such as 2427. Also the number of spaces in the input string can vary drastically.
Is there a way to do this?
Thanks in advance
CodePudding user response:
I found a solution by using an additional substring provided at the beginning of the input. Not the best, but it will work.
public static void createItems(String input)
{
// 25 chars before "SSID"
String[] lines = input.split("\r\n|\r|\n");
String ssid = lines[0].split("S ")[1];
ssid = ssid.substring(0, ssid.indexOf("C"));
String channel = lines[0].substring(ssid.length() 25);
channel = channel.substring(0, channel.indexOf("S"));
String signal = lines[0].substring(channel.length() ssid.length() 25);
signal = signal.substring(0, signal.indexOf("G") 1);
for(String s : lines)
{
if(!s.startsWith(" "))
{
items.add(new Item(
s.substring(25, 25 ssid.length()).trim(),
Integer.parseInt(s.substring(ssid.length() 25, ssid.length() 25 4)),
Integer.parseInt(s.substring(ssid.length() 25 5, ssid.length() 25 5 2)),
Integer.parseInt(s.substring(ssid.length() channel.length() 25, ssid.length() channel.length() 25 3))));
System.out.println(items.get(items.size() - 1));
}
}
}
input:
ADDRESS SSID CHANNEL SIG NF SNR RADIO-NAME ROUTEROS-VERSION
AP 02:3C:52:EC:61:00 vsat-24g-A6C2B0 2462/20/gn(18dBm) -82 -115 33
AP F8:CA:59:6D:29:56 AWI-500544 2412/20/gn(27dBm) -33 -103 70
AP DC:EF:09:E6:13:F4 Fender's Wifi_EXT 2427/20/gn(27dBm) -89 -104 15
AP 36:23:03:DA:30:50 _VelopEavenson-guest 2427/20/gn(27dBm) -89 -104 15
AP 30:23:03:DA:30:50 Eavenson Mesh 2427/20/gn(27dBm) -89 -104 15
AP 3A:23:03:DA:30:60 2442/20/gn(27dBm) -78 -102 24
AP 30:23:03:DA:30:60 Eavenson Mesh 2442/20/gn(27dBm) -90 -102 12
AP 36:23:03:DA:30:60 _VelopEavenson-guest 2442/20/gn(27dBm) -85 -102 17
AP 94:A6:7E:E6:3D:FA NETGEAR26 2.4Ghz 2452/20/gn(27dBm) -86 -104 18
AP 36:23:03:DA:30:C4 _VelopEavenson-guest 2462/20/gn(27dBm) -82 -104 22
AP 3A:23:03:DA:30:C4 2462/20/gn(27dBm) -81 -104 23
AP 30:23:03:DA:30:C4 Eavenson Mesh 2462/20/gn(27dBm) -80 -104 24
AP 3A:23:03:DA:30:50 2427/20/gn(27dBm) -91 -104 13
output:
[vsat-24g-A6C2B0,2462,20,-82]
[AWI-500544,2412,20,-33]
[Fender's Wifi_EXT,2427,20,-89]
[_VelopEavenson-guest,2427,20,-89]
[Eavenson Mesh,2427,20,-89]
[,2442,20,-78]
[Eavenson Mesh,2442,20,-90]
[_VelopEavenson-guest,2442,20,-85]
[NETGEAR26 2.4Ghz,2452,20,-86]
[_VelopEavenson-guest,2462,20,-82]
[,2462,20,-81]
[Eavenson Mesh,2462,20,-80]
[,2427,20,-91]
CodePudding user response:
Use regex to split with all spaces: "\s" - space " " - one or more
String first = "AP F8:CA:59:6D:29:56 AWI-500544 2412/20/gn(27dBm) -33 -103 70\n";
String[] arrayOfString = first.split("\\s ");
String string4 = arrayOfString[3];
String[] arrayOfString4 = string4.split("/");
String resultStr1 = arrayOfString[2];
String resultStr2 = arrayOfString4[0];
String resultStr3 = arrayOfString4[1];
String resultStr4 = arrayOfString[4];