检查java中的字符串中是否有辅音 [英] Check if there is a consonant in a string in java

查看:159
本文介绍了检查java中的字符串中是否有辅音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在编写一个名为pig latin的游戏程序(你可以查看维基百科的规则,但是它可以取得第一个辅音并将它们移到单词的后面并添加-ay)。我需要在单词的开头检查单词是否有任何辅音。如果它有一个或多个consant,它需要从第一个辅音开始,到元音之前的最后一个辅音结束。

So I am writing a program that is the game called pig latin (You can check Wikipedia for the rules, but it consits of taking the first consonants and moving them to the back of the word and adding -ay). I need to check the word has any consonants in the beginning of the word . If it has a or more consants the it needs to get the substring starting at the first consonant and ending at the last consonant before a vowel.

public void isConsonant(String word){
    char[] consonants = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm'
            , 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z' };
    char[] StringArray = word.toCharArray();
    for(int dex = 0; dex < StringArray.length; dex++ ){
        char current = StringArray[dex];
    }
}

以下是一个例子:

手套这个词应该变成过度。我的程序输出'lovegay'。

The word 'glove' should become oveglay . My program outputs 'lovegay'.

这是我的程序(减去进口 GUI stuff):

Here is my program (minus imports and GUI stuff):

public class PigLatin extends JFrame implements ActionListener {
    JTextField word = new JTextField(25);
    JButton GO = new JButton("Go");
    JLabel output = new JLabel();
    String CharNum1;
    String CharNum2;

    //The frame setup should be here

    public void isConsonant(String verify) {
        char[] consonants = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm',
                'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z' };
        char[] StringArray = verify.toCharArray();
        for (int dex = 0; dex < StringArray.length; dex++) {
            char current = StringArray[dex];
        }
    }

    public void game() {
        // The actual word
        String word2 = word.getText();

        // The first character
        CharNum1 = word2.substring(0, 2);
        CharNum2 = word2.substring(1, word2.length());

        // Remove the first char
        // String RevampedWord = word2.replace(CharNum1, "");
        String finalWord = CharNum2 + "-" + CharNum1 + "ay";
        output.setText(finalWord);
    }

    public void actionPerformed(ActionEvent event) {
        game();
    }

    public static void main(String[] arguments) {
        PigLatin pig = new PigLatin();
    }
}

所以我的问题是天气可以看出是否一个String包含consonats并在元音之前为第一个辅音制作一个辅音字符串。

So my question is weather it is possible to see if a String contains consonats and make a substring of the first consonant to the first consonant before a vowel.

提前致谢。

推荐答案

可能做你想做的事情的一种方式有点吸引人:

One way of possibly doing what you want that is a little intriguing:

假设您将 word 转换为猪拉丁语,那么使用 word.split([aeiouAEIOU],2)?查看 split 的文档,它将恰好匹配正则表达式一次,因此返回的字符串数组将被拆分为单词中的第一个元音。然后,您可以根据第一个元音是第一个字符,最后一个字符,介于两者之间,还是无处可寻,将这些片段放回正确的顺序(请记住,并非所有单词都包含这五个元音中的一个)。

Supposing that you are turning word into pig latin, what about using word.split("[aeiouAEIOU]", 2)? Looking at the docs for split, it'll match the regex exactly once, so the array of strings returned will be split around the first vowel in the word. You can then put those pieces back into the right order based on whether the first vowel was the first char, last char, somewhere in between, or even nowhere to be found (remember, not all words contain one of those five vowels).

(注意 split 会删除第一个元音,如果找到它,所以你必须使用 word 将元音返回,但第一段的大小也将是元音的索引 word 。)

(Note that split will remove the first vowel if it finds it, so you'll have to use word to get that vowel back, but the size of the first piece will also be that vowel's index into word.)

这篇关于检查java中的字符串中是否有辅音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆