Pregunta

I'm wondering about how I can do something like this:

String[] emoticon;
emoticon=e.split(":)");

Is it possible to be done with split(); or is there another way around?

¿Fue útil?

Solución

Assuming you are trying to use the smiley face :) as a delimiter, there is a consideration you have to make: split() accepts regex, so you have to escape (with \\) any special characters you use (including but not limited to ()[]+*):

emoticon=e.split(":\\)");

Proof-of-concept on IDEOne

Otros consejos

Use Pattern.quote:

emoticon = e.split(Pattern.quote(":)"));

It will surround your String with \Q and \E and escape any \E substrings within your pattern.

you need to escape the braces:

emoticon=e.split(":\\)");

but you know, this will get rid of the emoticon, right?

to retrieve it out of some text you'll need something like this:

List<String> emoticons = new ArrayList<String>();
// adjust regex to find more emoticons
Pattern pattern = Pattern.compile(":\\)");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    emoticons.add(matcher.group());
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top