I need to parse this into strings in the most efficient way. I currently have this line
D[date-string] T[time-string] N[name-string] M[message-string]
Needs to be parsed into four strings respectively,
private String time; //would equal time-string
private String date; //would equal date-string
private String name; //would equal name-string
private String message; //would equal message-string
Is there an easy way to do this?
You can use regex and groups to match and extract that kind of data:
For example, something like:
Pattern p = Pattern.compile("D(\\w+) T(\\w+) N(\\w+) M(\\w+)");
Matcher m = p.matcher(yourString);
if (m.matches()){
String d = m.group(1);
String t = m.group(2);
String n = m.group(3);
String w = m.group(4);
}
The patterns within the parentheses are saved into groups, which you can extract after the match (it starts with 1, since 0 is the whole match).
You have then to change that to the characters and patterns you want to accept.
I'm not sure if I've understood you but looks like using regular expression is the easiest way to do this kind of parsing.
The below logic will work. How regix will hep not sure. But the below code will also be ok as it is not looping and anything so i think it will could be the optimum solution.
String s = "D[date-string] T[time-string] N[name-string] M[message-string]";
String[] str = s.split(" ");
String date = str[0].substring(str[0].indexOf("[")+1, str[0].lastIndexOf("]")); //would equal time-string
String time=str[1].substring(str[1].indexOf("[")+1, str[1].lastIndexOf("]")); //would equal date-string
String name=str[2].substring(str[2].indexOf("[")+1, str[2].lastIndexOf("]")); //would equal name-string
String message=str[3].substring(str[3].indexOf("[")+1, str[3].lastIndexOf("]"));
Wow, easy easy easy!
D[(.*?)] for the date!
https://regex101.com/r/vM0yW8/1
Take a look here and here, you will have to tune it accordingly.
Related
Need help with getting rid of half of a string in android studio. The string is:
final String strOrigin = String.valueOf(origin).trim();
The value that is returned is;
"Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}"
I want to be left with only the numbers of that, in the String. I have tried;
strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
But it isn't working. Any help would be appreciated.
In Java strings are immutable. You must assign the result to a new string:
String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
Make your life simpler by just getting the coordinates:
final String strOigin = origin.coordinates().stream()
.map(String::valueOf)
.collect(Collectors.joining(",","{","}"));
Or, if you're stuck on java 7:
final String strOigin = String.format(
"{%s,%s}",
String.valueOf(origin.latitude()),
String.valueOf(origin.longitude())
);
Maybe you forgot that replace() returns the result and you must assign it to a string:
String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
String str = strOrigin
.replace("Point{type=Point, bbox=null, coordinates=", "")
.replace("}","");
System.out.println(str);
will print:
[27.993726079654873,-26.14686805145815]
This should do the trick
String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));
will print:
27.993726079654873,-26.14686805145815
Than you can cast it to double or latlong format
Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:
String c = origin.replaceAll(".+?(\\[.+?\\]).+?", "$1");
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
I want to parse the following and store it as a new string, with the condition that mawi is stored and everything else is removed.
<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>
One solution I suppose could be a substring starting with the first character after the first > and ending two characters before the first -. All the data is identical. The result is a String with value mawi.
String initial = "<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>";
String substring = initial.substring(example.indexOf(">"));
Not sure where to go from here... Any thoughts?
Although the below code do the trick, I suggest you to use Jsoup or XML Parse if you are processing multiple strings like this
Pattern pattern = Pattern.compile("<ns0:Assignee>(.+?)</ns0:Assignee>");
Matcher matcher = pattern.matcher("<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>");
matcher.find();
String result = matcher.group(1);
String finalString = result.split(" - ")[0];
System.out.println(finalString); // mawi
If all the strings are built like your example string, you could go with this:
initial.substring(initial.indexOf('>') + 1, initial.indexOf(' '));
Note the + 1 at the start index.
When your Strings are more complicated, I would recommend either using a library for working with XML or using Regular Expressions.
So now you got substring which is equal to: >mawi - Manfred Wilson</ns0:Assignee>.
Now, you can substring your substring again to find only mawi, like this;
String initial = "<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>";
String midSub = initial.substring(initial.indexOf('>'));
String finalSub = midSub.substring(1, midSub.indexOf(' ')); // 1 because we still have `>`
System.out.println(finalSub);
Or, one liner:
String finalSub = initial.substring(initial.indexOf('>')+1, initial.indexOf(' '));
show this:
String s = "<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>";
s = s.substring(s.indexOf("<ns0:Assignee>")+"<ns0:Assignee>".length(), s.indexOf("</ns0:Assignee>"));
public class string {
public static void main(String[] args) {
String s = "<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>";
s = s.substring(14, 18);
System.out.println(s);
}
}
I have a string Till No. S59997-RSS01 Now I need to extract the 01 from it , but the issue is that it is dynameic means
String TillNo =pinpadTillStore.getHwIdentifier();
The value S59997-RSS01 is in TillNo, that I come to know from debugging but in real time which value is coming inside TillNo , will not be known to me but the pattern of the value will be the same (S59997-RSS01) , Please advise how to extract the last two digits like(01)
int size = tillNo.length();
String value = tillNo.substring(size-2); // do this if size > 2.
You can use the subString method.
Refer to how to use subString()
If the two digits will only appear at last two position, just use the substring method. For a more flexible way, use Regular Expression instead.
String TillNo = "S59997-RSS01";
System.out.println(TillNo.substring(TillNo.length() - 2));
Pattern pattern = Pattern.compile("S[\\d]{5}-RSS([\\d]{2})");
Matcher matcher = pattern.matcher(TillNo);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
exactly, you can extract the last 2 digits using the substring method for strings like:
String TillNo="S59997-RSS01";
String substring=TillNo.substring(TillNo.length()-2,TillNo.length());
I have a string "content/users/user/missions/mission" .I need to get "content/users/user/missions" from it [i.e. string upto the last delimiter] .How to proceed ?
If your requirement is that simple then you could do the following:
String string = "content/users/user/missions/mission";
String newString = string.substring(0, string.lastIndexOf('/'));
There are more fancy ways of doing this, regex could be one.
Use lastIndexOf and substring methods from String class.
String str = "content/users/user/missions/mission";
String result = str.substring(0,str.lastIndexOf('/'));
Hi there is a requirement to strip a string along backslash(/)
For example I have
String vret = "Comment Four/Y/34147/D_Z";
This has to be splitted into 4 string namely
Str sarr[]={comment,Y,34147,D_Z}
The string will be always in this format only like XXXXX/X/XXXXXX/XXX:
the first part will be alphanumeric value,
the second part is single character
the third is always a number
the fourth is any character
I am assuming there will be regex to do this kind of operations in java
What about
String[] sarr = vret.split("/");
Oracle even has the documentation.
using full regex and pre-compiled pattern
Pattern p = Pattern.compile("([\\w]+)/(\\w)/(\\d+)/(\\.*)");
Matcher m = p.matcher(vret);
if(m.matches()){
String first = m.group(1);
String second= m.group(2);
int third = Integer.parseInt(m.group(3));
String fourth= m.group(4);
}
String sarr[] = vret.split("/");
Simple, uh ?