H
H
Hello America2014-06-09 11:52:23
Java
Hello America, 2014-06-09 11:52:23

Java language. Split a string into substrings by a specific delimiter?

There is a task - to break the term and drive it into an array according to a certain separator

String[] arrayMessage;
String textMessage = "David>Mark:Привет Mark, как дела?[message]Login>David:Привет David, как дела?[message]
Login>Lenan:Привет Lenan, как дела?[message]Kriss>Serega:Привет Serega, как дела?[message]";
//В этой строке [message] является разделителем подстрок
//Необходимо загнать так, чтобы получилось вот так
arrayMessage[0] = "David>Mark:Привет Mark, как дела?";
arrayMessage[1] = "Login>David:Привет David, как дела?";
arrayMessage[2] = "Login>Lenan:Привет Lenan, как дела?";
arrayMessage[3] = "Kriss>Serega:Привет Serega, как дела?";

Tried with split() but didn't work, splits incorrectly.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
bimeg, 2014-06-09
@websofter

String[] arrayMessage = textMessage.split("\\[message\\]");

O
one pavel, 2014-06-09
@onepavel

The matter is that it is necessary to escape square brackets in the regular expression and then everything will turn out =)
textMessage.split("\\[message\\]");

H
Hello America, 2014-06-09
@websofter

In short, I realized that I need to come up with a regular expression for split (), but I don’t know how

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question