My String is as below
String responseBody = ["{\"event\":{\"commonEventHeader\":{\"sourceId\":\"\",\"startEpochMicrosec\":\"1590633627120000\",\"eventId\":\"135.16.61.40-Fault_bgp_neighbor_adjacency_down-192.20.126.67\",\"internalHeaderFields\"}"]
I want to split this string by event\":
I am trying below :
String[] json = responseBody.split("event\":");
This is not able to split , I am not getting any error too . Please suggest .
I'm confused as to why you wouldn't try to parse the JSON since it looks like you know it is JSON. But in the spirit of answering the actual question, I think it's because the string you are trying to split actually contains the \ character, and therefore you should use:
String[] json = responseBody.split("event\\\\\":");
Why so many \? Well the actual regex is event\\": but in Java, escape each \ and the ".
Use Built-in String method split("regex").
Example:
String s = "This is a String";
//Split String
String[] arr = s.split(" ");
Related
I have a following string:
Hello word!!!
or
Hello world:)
Now I want to split this string to an array of string which contains Hello,world,!,!,! or Hello,world,:)
the problem is if there was space between all the parts I could use split(" ")
but here !!! or :) is attached to the string
I also used this code :
String Text = "But I know. For example, the word \"can\'t\" should";
String[] Res = Text.split("[\\p{Punct}\\s]+");
System.out.println(Res.length);
for (String s:Res){
System.out.println(s);
}
which I found it from here but not really helpful in my case:
Splitting strings through regular expressions by punctuation and whitespace etc in java
Can anyone help?
Seems to me like you do not want to split but rather capture certain groups. The thing with split string is that it gets rid of the parts that you split by (so if you split by spaces, you don't have spaces in your output array), therefore if you split by "!" you won't get them in your output. Possibly this would work for capturing the things that you are interested in:
(\w+)|(!)|(:\))/g
regex101
Mind you don't use string split with it, but rather exec your regex against your string in whatever engine/language you are using. In Java it would be something like:
String input = "Hello world!!!:)";
Pattern p = Pattern.compile("(\w+)|(!)|(:\))");
Matcher m = p.matcher(input);
List<String> matches = new ArrayList<String>();
while (m.find()) {
matches.add(m.group());
}
Your matches array will have:
["Hello", "world", "!", "!", "!", ":)"]
Say I enter a string:-
Hello
Java!
I want the output as:-
Hello\nJava!
Is there any way in which I could get the output in this format?
I am stuck on this one and not able to think about any logic which could do this for me.
It looks like http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html does what you want.
EG
String escaped = StringEscapeUtils.escapeJava(String str)
Will return "Hello\nJava!" when supplied with your string.
Hm..You can replace the new line character(\n) with \\n. For example:
String helloWithNewLine = "Hello\nJava";
String helloWithoutNewLine = helloWithNewLine.replace("\n", "\\n");
System.out.println(helloWithoutNewLine);
Output:
Hello\nJava
In my project I used the code to split string like "004*034556" , code is like below :
String string = "004*034556";
String[] parts = string.split("*");
but it got some error and force closed !!
finally I found that if use "#" or another things its gonna work .
String string = "004#034556";
String[] parts = string.split("#");
how can I explain this ?!
Your forgetting something very trivial.
String string = "004*034556";
String[] parts = string.split("\\*");
I recommend you check out Escape Characters.
Use Pattern.quote to treat the * like the String * and not the Regex * (that have a special meaning):
String[] parts = string.split(Pattern.quote("*"));
See String#split:
public String[] split(String regex)
↑
Refer JavaDoc
String[] split(String regex)
Splits this string around matches of the given regular expression.
And the symbol "*" has a different meaning when we talk about Regex in Java
Thus you would have to use an escape character
String[] parts = string.split("\\*");
I have a string like this:
String str="\"myValue\".\"Folder\".\"FolderCentury\"";
Is it possible to split the above string by . but instead of getting three resulting strings only two like:
columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";
Or do I have to use an other java method to get it done?
Try this.
String s = "myValue.Folder.FolderCentury";
String[] a = s.split(java.util.regex.Pattern.quote("."));
Hi programmer/Yannish,
First of all the split(".") will not work and this will not return any result. I think java String split method not work for . delimiter, so please try java.util.regex.Pattern.quote(".") instead of split(".")
As I posted on the original Post (here), the next code:
String input = "myValue.Folder.FolderCentury";
String regex = "(?!(.+\\.))\\.";
String[] result=input.split(regex);
System.out.println("result: "+Arrays.toString(result));
Produces the required output (an array with two values):
result: [myValue.Folder, FolderCentury]
If the problem you're trying to solve is really that specific, you could do it even without using regular expression matches at all:
int lastDot = str.lastIndexOf(".");
columnArray[0] = str.substring(0, lastDot);
columnArray[1] = str.substring(lastDot + 1);
I have input string in the following format
first|second|third|<forth>|<fifth>|$sixth I want to split this string into an array of string with value [first,second,third,,,$sixth]. I am using following code to split the string but that is not working. please help me.
public String[] splitString(String input){
String[] resultArray = input.split("|")
return resultArray;
}
Could you please tell me what am I doing wrong.
You need to escape | using backslash as it is a special character. This should work:
String[] resultArray = input.split("\\|")
| is a meta character meaning it represents something else in regex. Considering split takes regex as an argument, it interprets the argument using regex. You need to "escape" all of the meta characters by placing a \\ before it. In your case, you would do:
String[] resultArray = input.split("\\|");