简体   繁体   English

将字符串分成两部分

[英]Splitting a string into two

I am attempting to split a word from its punctuation: 我正在尝试从标点符号中拆分一个单词:

So for example if the word is "Hello?". 例如,如果单词是“ Hello?”。 I want to store "Hello" in one variable and the "?" 我想将“ Hello”存储在一个变量中,并将“?”存储在 in another variable. 在另一个变量中。

I tried using .split method but deletes the delimiter (the punctuation) , which means you wouldn't conserve the punctuation character. 我尝试使用.split方法,但是删除了定界符(标点符号),这意味着您将不会保留标点符号。

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 ideone演示

Further to the other suggestions, you can also use the 'word boundary' matcher '\\b'. 除了其他建议,您还可以使用“单词边界”匹配器“ \\ 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 这可能并不总是与您要查找的内容匹配,它会检测到单词和非单词之间的边界,如记录所示: 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 您可以看到它在这里运行: http : //ideone.com/3GmgqD

I've also fixed your .contains to use .matches instead. 我还修复了.contains改用.matches

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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