【问题标题】:How to split a string or get the right text from string [duplicate]如何拆分字符串或从字符串中获取正确的文本[重复]
【发布时间】:2014-04-14 12:23:42
【问题描述】:

我需要一些帮助来拆分字符串。现在,我的字符串包含以下内容:

"RM 8 Text" 

现在我只想在我的字符串生成器中使用 append 打印文本,如何去掉字符串开头的 RM 8?

现在我已经这样做了,但必须有更简单的方法。

String[] lines = fromServer.split("\\s+");
String line2 = lines[2];    
logbuilder.append(line2);

【问题讨论】:

  • logbuilder.append(fromServer.substring(5)
  • @staticx OP 已经做到了。寻找更好的方法,。
  • @sᴜʀᴇsʜᴀᴛᴛᴀ:仍然是重复的。
  • @staticx 不。我不这么认为:)
  • @staticx 如果问题相似并且很可能不会得到完全相同的答案,那么您可能没有重复。一个特定的案例,所以 OP 不会得到类似的答案,但答案适用于 他的 场景。对我来说,这不是重复的。

标签: java


【解决方案1】:

假设是标准格式

String lastWord = fromServer.substring(fromServer.lastIndexOf(" ")+1);

【讨论】:

  • 非常感谢...这很好:)
  • @Pixel 不客气。
【解决方案2】:

您可以简单地替换 RM 8

String content = fromServer.replaceAll("RM 8", "");

logBuilder.append(content);

【讨论】:

  • logBuilder.append(fromServer.replceAll("RM 8", "")); ;)
  • 我尝试使我的更改尽可能接近 OP 代码。因此有两条线解决方案。
【解决方案3】:

试试这个

s = s.replaceAll(".*\\s+(\\S+)$", "$1")

【讨论】:

    【解决方案4】:

    试试下面的代码示例,

    String abc = "RM 8 Text" ;
    int a = abc.lastIndexOf(" ");
    String xyz = abc.substring(a,abc.length());
    System.out.println(xyz);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多