2

我有一个看起来像这样的字符串。

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 "

我想将此字符串拆分为部分。基本上我需要之后的部分

^^^^^^纳米时间的延迟:

那将是:

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 

我尝试了什么:

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

但这对我没有用。任何帮助,将不胜感激。

4

4 回答 4

2

拆分不起作用的原因是因为'^'是正则表达式中的元字符。 http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

如果你真的想使用拆分,你应该转义'^'字符

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

在这种情况下,最好只使用 indexOf

于 2013-11-08T18:54:36.783 回答
2

我需要之后的部分...

这让我与indexOf而不是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.
}
于 2013-11-08T18:38:24.363 回答
1

拆分后,只需获取第二个元素splitted[1]。在你分手之后,这应该会给你一切。

存在一些安全问题,但如果输入的形式始终保持不变,它应该可以工作。


一些建议:

确保输入包含您要拆分的字符串,并且后面有内容。

-或者-

在获取第二个元素之前,确保 的长度splitted至少为 2。


此外,输出Sysout(splitted)可能不正确的原因是因为toString()数组的默认值。打印数组时应该遵循这一点。

于 2013-11-08T18:34:40.060 回答
1

如果您的“分隔符”是恒定的,您可以使用您的分隔符 + 其长度来制作substring原始文件。StringindexOf

下面非常丑陋的例子:

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

输出:

baz

笔记:

这不会任何方式分裂你String。它只返回您的“分隔符”之后的内容。

还要注意错误:

  • 如果您的分隔符不包含在 originalString中,代码将返回您的 originalString的子字符串,从-1+ 您的分隔符的长度开始
  • 如果前面的条件适用,并且如果-1+ 你的分隔符的长度大于你的String长度,代码将StringIndexOutOfBoundsException在运行时抛出
于 2013-11-08T18:40:01.913 回答