简体   繁体   中英

Splitting a string into two

I am attempting to split a word from its punctuation:

So for example if the word is "Hello?". I want to store "Hello" in one variable and the "?" in another variable.

I tried using .split method but deletes the delimiter (the punctuation) , which means you wouldn't conserve the punctuation character.

String inWord = "hello?";
String word;
String punctuation = null;
if (inWord.contains(","+"?"+"."+"!"+";")) {
    String parts[] = inWord.split("\\," + "\\?" + "\\." + "\\!" + "\\;");
    word = parts[0];
    punctuation = parts[1];
} else {
    word = inWord;
}

System.out.println(word);
System.out.println(punctuation);

I am stuck I cant see another method of doing it.

Thanks in advance

You could use a positive lookahead to split so you don't actually use the punctuation to split, but the position right before it:

inWord.split("(?=[,?.!;])");

ideone demo

Further to the other suggestions, you can also use the 'word boundary' matcher '\\b'. This may not always match what you are looking for, it detects the boundary between a word and a non-word, as documented: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

In your example, it works, though the first element in the array will be a blank string.

Here is some working code:

String inWord = "hello?";
String word;
String punctuation = null;
if (inWord.matches(".*[,?.!;].*")) {
    String parts[] = inWord.split("\\b");
    word = parts[1];
    punctuation = parts[2];
    System.out.println(parts.length);
} else {
    word = inWord;
}

System.out.println(word);
System.out.println(punctuation);

You can see it running here: http://ideone.com/3GmgqD

I've also fixed your .contains to use .matches instead.

I think you can use the below regex. But not tried. Give it a try.

input.split("[\\p{P}]")

You could use substring here. Something like this:

    String inWord = "hello?";
    String word = inWord.substring (0, 5);
    String punctuation = inWord.substring (5, inWord.length ());

    System.out.println (word);
    System.out.println (punctuation);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM