2

I have a string that would look like this .

String str="some data some data some data some data some data some data 
some data some data some data some data some data some data some data 
some data some data some data some data some data some data 
some data some data some data some data some data 
some data some data some data some data some data 
YLOG^2Fri Nov 08 00:58:59 PST 2013^^^^^^^^^^^^^^^^^^^^^latency in nano time : 264414262^
YLOG^2Fri Nov 08 00:58:59 PST 2013^^^^ XML after modification :<site name="some data ">
some data some data some data some data some data some data some data 
some data some data some data some data some data some data 
some data some data some data some data some data 
some data some data some data some data some data some data some data some data some data some data some data some data 
some data some data some data some data some data some data 
some data some data some data some data some data 
some data some data some data some data some data "

I would like to split this string in to parts . Basically I need the part after

^^^^^^latency in nano time :

That would be:

264414262^
    YLOG^2Fri Nov 08 00:58:59 PST 2013^^^^ XML after modification :<site name="some data ">
    some data some data some data some data some data some data some data 
    some data some data some data some data some data some data 
    some data some data some data some data some data 
    some data some data some data some data some data some data some data some data some data some data some data some data 
    some data some data some data some data some data some data 
    some data some data some data some data some data 
    some data some data some data some data some data 

What I tried :

String[] splited = str.split("^^^^^latency in nano");
Sysout(splitted);

But that didn't work out for me. Any help would be appreciated.

rahul888
  • 413
  • 2
  • 8
  • 18

4 Answers4

2

I need the part after ...

This has me going with indexOf as opposed to split

public String getSub(String orig){
    String delim = "^^^^^latency in nano";

    int idx = orig.indexOf(delim);
    if(idx != -1){
       return orig.substring(idx + delim.length());
    }
    return orig; // delim not found.
}
MadConan
  • 3,749
  • 1
  • 16
  • 27
2

The reason why the split didn't work is because '^' is a metacharacter in regular expressions. http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

If you really want to use split, you should escape '^' character

String[] splitted = str.split("\\^\\^\\^\\^\\^latency in nano");
System.out.println(splitted[1];

In this case it is probably better just to use indexOf

Tanya Kogan
  • 141
  • 2
  • 3
1

Once you split, just get the second element splitted[1]. That should give you everything after what you split on.

There are some safety issues, but it should work if the input is always constant in form.


Some recommendations:

Ensure the input contains the string you are splitting and there is content after.

-or-

Ensure the length of splitted is at least two before getting the second element.


Also, the reason why the output in Sysout(splitted) may not have been correct is because of the default toString() of arrays. You should follow this when printing arrays.

Community
  • 1
  • 1
Obicere
  • 2,999
  • 3
  • 20
  • 31
  • tried getting this : <![CDATA[java.lang.ArrayIndexOutOfBoundsException: 1 – rahul888 Nov 08 '13 at 18:38
  • @rahul888 You need to ensure one of the two conditions is met: The input contains the string you are looking for and has content after or the length of the splitted array is at least two. – Obicere Nov 08 '13 at 18:40
1

If your "delimiter" is constant, you can substring your original String with the indexOf your delimiter + its length.

Very ugly example below:

String s = "blah^^^^^^^latency in nano time : baz";
System.out.println(
        s.substring(
                s.indexOf("^^^^^^^latency in nano time : ") + "^^^^^^^latency in nano time : ".length()
        )
);

Output:

baz

Note:

This does not split your String in any way. It just returns what's after your "delimiter".

Also watch out for bugs:

  • If your delimiter is not contained in the original String, the code will return your original String's sub-string starting from -1 + your delimiter's length
  • If the previous condition applies, and if -1 + your delimiter's length is larger than your String length, the code will throw a StringIndexOutOfBoundsException at runtime
Mena
  • 47,782
  • 11
  • 87
  • 106