如果p,则将字符串拆分为两部分

时间:2014-03-29 12:21:38

标签: java string split

我试图从标点符号中删除一个单词:

例如,如果单词是"Hello?"。我想将"Hello"存储在一个变量中,将"?"存储在另一个变量中。

到目前为止,这是我的代码:

    String inWord = "hello?";
    if (inWord.contains(","+"?"+"."+"!"+";")) {

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

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

我的问题是我收到错误:当我尝试打印出单词和标点符号时找不到符号。

提前感谢您的帮助

5 个答案:

答案 0 :(得分:0)

您的代码还有其他问题,但您的问题是您收到“无法找到符号”错误的原因。

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

String word = ...之类的变量声明的范围只是它所在的块(“{”和“}”内的代码片段)。变量wordpunctuation在您尝试打印它们的范围内不存在。

您需要声明您的变量wordpunctuation在您System.out.println <中访问它们的相同范围(或封闭范围)中/ p>

答案 1 :(得分:0)

您可以尝试使用自定义包含功能和StringTokenizer 为:

    public class Test{

    public static void main(String[] args) {
        String inWord = "hello";
        String[] wordAndPunctuation = null;
        char[] punctuations =new char[]{',','?','.','!',';'};
        StringTokenizer tokenizer = new StringTokenizer(inWord,new String(punctuations),true);
        int i = 0;

        if (Test.contains(inWord,punctuations)) {
            while(tokenizer.hasMoreTokens()){
                wordAndPunctuation = new String[tokenizer.countTokens()];
                System.out.println(tokenizer.countTokens());
                wordAndPunctuation[i] = tokenizer.nextToken();
                i++;
            }
        }else{
            System.out.println("No punctuation in "+inWord);
        }
    }

    public static boolean contains(String str, char[] charArr){
        System.out.println(Arrays.toString(charArr));
        for(char c:charArr){
            if(str.contains(String.valueOf(c)))
            return true;
        }
        return false;
    }
}

答案 2 :(得分:0)

您在代码中犯了以下错误。

1.声明if条件之外的字符串

2的 inWord.contains(&#34;&#34; +&#34;&#34; +&#34;&#34; +&#34;!&#34 ; +&#34;;&#34;)这等于 inword.contains(&#34;,?。!;&#34;),所以条件会总是失败,它会进入其他条件

  1. split()不会存储基于您拆分字符串
  2. 的值

    例如

     String string = "004-034556";
    
     String[] parts = string.split("-");
    
     String part1 = parts[0]; // 004
    
     String part2 = parts[1]; // 034556
    

    这个值&#34; - &#34;无法存储。希望你能理解我想传达的内容。

答案 3 :(得分:0)

我建议解析String并检查字符是否为标点符号方法:

String sentence = "Hello? Is this Mrs. Doubtfire?"; // Example.
ArrayList<String> chunks = new ArrayList<>();   // Will store the "non-punctuated chunks"
ArrayList<Character> puncts = new ArrayList<>();// Will the punctuations in the "sentence"
char[] punctuations = {',','?','.','!',';'};    // Store punctuations here.
int lastIndex = 0;
for (int i = 0; i < sentence.length(); i++) {
    char c = sentence.charAt(i);
    for (char punctuation : punctuations) {
        if (c == punctuation) {
            chunks.add(sentence.substring(lastIndex, i).trim());
            puncts.add(c);
            lastIndex = i + 1;
        }
    }
}
System.out.println(chunks);
System.out.println(puncts);

输出:

[Hello, Is this Mrs, Doubtfire]
[?, ., ?]

请记得import java.util.ArrayList

答案 4 :(得分:0)

你为什么不这样做:

String s = "hello!";

Pattern p = Pattern.compile("(\\w+)?(\\W)");
Matcher m = p.matcher(s);
while (m.find()) {
    System.out.println("Word: " + m.group(1) + " | Punctuation: " + m.group(2));
}

Group1将包含单词,Group2将包含标点符号。

演示:http://ideone.com/ljIZFW