0

I need some help to split up a string. Right now, my string contains this:

"RM 8 Text" 

Now I only want Text to be printed in my string builder using append, how do I get rid of the RM 8 in the start of the string?

Right now I have done it this way, but there has to be an easier way.

String[] lines = fromServer.split("\\s+");
String line2 = lines[2];    
logbuilder.append(line2);
nbanic
  • 1,270
  • 1
  • 8
  • 11
Pixel
  • 349
  • 1
  • 8
  • 17
  • `logbuilder.append(fromServer.substring(5)` – kai Apr 14 '14 at 12:26
  • 2
    @staticx OP already did it. Looking for better way,. – Suresh Atta Apr 14 '14 at 12:26
  • @sᴜʀᴇsʜᴀᴛᴛᴀ: It's still a duplicate. – Engineer2021 Apr 14 '14 at 12:27
  • 1
    @staticx Nope. I don't think so :) – Suresh Atta Apr 14 '14 at 12:27
  • @sᴜʀᴇsʜᴀᴛᴛᴀ: Yep, it is. Check Meta for an explanation. – Engineer2021 Apr 14 '14 at 12:28
  • @staticx I'm not that much active on meta. Could you please attach the post that explains it ? – Suresh Atta Apr 14 '14 at 12:29
  • @staticx It's not a duplicate. `duplicate` means that the OP is asking the same thing as the other question. That's not the case, the OP already knows how to split a string by spaces, he's simply searching for an easier solution. – BackSlash Apr 14 '14 at 12:29
  • @BackSlash: https://meta.stackexchange.com/a/12184/139168 – Engineer2021 Apr 14 '14 at 12:29
  • @sᴜʀᴇsʜᴀᴛᴛᴀ: https://meta.stackexchange.com/a/12184/139168 – Engineer2021 Apr 14 '14 at 12:30
  • @sᴜʀᴇsʜᴀᴛᴛᴀ: Uhm, "if you are likely to get similar answers.." yes, yes, it is :) – Engineer2021 Apr 14 '14 at 12:31
  • 1
    @staticx ***If the question is similar and it is likely to NOT get the exact same answer, you likely do not have a duplicate.*** That's a specific case, so the OP won't get similar answers, but answers applied to ***his*** scenario. To me it's not a duplicate. – BackSlash Apr 14 '14 at 12:32
  • @staticx Again, OP know how to split and he already did it. OP looking for a fair way. – Suresh Atta Apr 14 '14 at 12:32
  • @BackSlash: If you say so, it's a dupe. It's likely to get the same answer. – Engineer2021 Apr 14 '14 at 12:32
  • Plus, here are some more: https://stackoverflow.com/questions/19600876/split-string-by-space-java and https://stackoverflow.com/questions/23059993/how-to-split-a-string-or-get-the-right-text-from-string?noredirect=1#comment35237287_23059993 and https://stackoverflow.com/questions/19019560/java-splitting-a-string-by-whitespace-when-there-is-a-variable-number-of-whites how many times are we going to answer the same thing? – Engineer2021 Apr 14 '14 at 12:33
  • @staticx **Splitting string by " " != getting last word after split with " "** – Suresh Atta Apr 14 '14 at 12:34
  • @Pixel could you please consider changing of title, which exactly reflect the problem. – Suresh Atta Apr 14 '14 at 12:36
  • @staticx To me it's not a duplicate, we are not simply talking about splitting a string by spaces, we are talking about getting the last part of a string in an easier way than splitting (If you read the question, you'll see that the OP is asking for a better way to solve his scenario ***without*** splitting). – BackSlash Apr 14 '14 at 12:36
  • @BackSlash: Using that logic, then here: https://stackoverflow.com/questions/4672806/java-fastest-way-to-get-last-word-in-a-string :) – Engineer2021 Apr 14 '14 at 12:39

4 Answers4

4

Assuming that is standard format

String lastWord = fromServer.substring(fromServer.lastIndexOf(" ")+1);
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

You could simply replace the RM 8.

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

logBuilder.append(content);
christopher
  • 26,815
  • 5
  • 55
  • 89
1

try this

s = s.replaceAll(".*\\s+(\\S+)$", "$1")
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

Try the following code sample,

String abc = "RM 8 Text" ;
int a = abc.lastIndexOf(" ");
String xyz = abc.substring(a,abc.length());
System.out.println(xyz);
astack
  • 3,837
  • 7
  • 22
  • 21