【问题标题】:How to split a sentence for every "," symbol?如何为每个“,”符号拆分一个句子?
【发布时间】:2020-01-15 03:52:43
【问题描述】:

我知道使用split函数来分割句子的正常方法,但问题是你需要声明你需要多少个变量, 示例:格斗、动作、冒险、竞速、角色扮演

String[] GameGenreCodeSeparated = GameGenreCodeRAW.split(",");
listGameGenre.add(GameGenreCodeSeparated[0]);
listGameGenre.add(GameGenreCodeSeparated[1]);
listGameGenre.add(GameGenreCodeSeparated[2]);

如何为每个','符号添加一个列表,以便一个列表可以动态包含该句子中的5个对象,有什么解决方案吗?

【问题讨论】:

    标签: java android arrays split


    【解决方案1】:

    你会想要遍历你的数组。像下面这样的东西应该可以工作。

    String[] GameGenreCodeSeparated = GameGenreCodeRAW.split(",");
    for (String GameGenre: GameGenreCodeSeparated ) { 
         listGameGenre.add(GameGenre);
    } 
    

    【讨论】:

    • 谢谢,这是一个更清晰的解释和完美的工作
    【解决方案2】:

    使用内置方法Arrays.asList(GameGenreCodeRAW.split(","))避免手动添加。

    【讨论】:

      【解决方案3】:

      @Sungakki 您应该检查哪种方法更好,在这种情况下,Arrays.asList(GameGenreCodeRAW.split(",")) 看起来更好,将来也可以帮助您。 原因是 Arrays.asList() 是处理数组的标准实用方法。在这里编写自定义 for() 循环没有意义。

      如果您难以理解它在做什么,请参阅下面的详细信息。

      这是您将如何在代码中使用它的方法。

      listGameGenre = Arrays.asList(GameGenreCodeRAW.split(","));

      Arrays.asList() 只返回一个由指定数组支持的固定大小的列表,在我们的例子中它是 GameGenreCodeRAW。

      这里是Arrays.asList() official docs 的官方文档。

      希望你明白我的意思。

      【讨论】:

        【解决方案4】:

        最好创建一个新数组或数组列表,因为尝试拆分字符串但不将任何数据存储在其他任何地方会很麻烦。 我认为应该是这样的:

        import java.util.ArrayList;
            public class test 
            {
                public static void split(String raw){
                    ArrayList <String>  GameGenreCodeSeparated= new ArrayList<String>();
                    int splits=0;//how man times have the sentence been splited
                    int previousSplited=0;//index of the prevoius comma
                    for(int i=0;i<raw.length();i++){
                        if(raw.charAt(i)==','){//when comma occurs
                            if(previousSplited==0){//if this is the first comma
                                GameGenreCodeSeparated.add(raw.substring(0,i));//add the splited string to the list
                                previousSplited=i;//record the place where splited
                            }
                            else{
                                GameGenreCodeSeparated.add(raw.substring(previousSplited+1,i));//add the splited string to the list
                                previousSplited=i;//record the place where splited
                            }
                        }
                        else if (i==raw.length()-1){//if this is the end of the string
                            GameGenreCodeSeparated.add(raw.substring(previousSplited+1,i));//add the splited string to the list
                            previousSplited=i;//record the place where splited
                        }
        
                    }
                    System.out.println(GameGenreCodeSeparated);//print out results;
                }
            }
        

        希望这会有所帮助!

        【讨论】:

          【解决方案5】:
              String text = "In addition, Androids could multitask, whereas iPhones could not at that time. By 2011, Android outsold every other smartphone";
          
              String[] textSeparated = text.split(",");
          
          String first=textSeparated[0];
          String second=textSeparated[1];
          String Third=textSeparated[2];
          String Fourth=textSeparated[3]; 
          

          【讨论】:

            猜你喜欢
            • 2019-09-22
            • 1970-01-01
            • 2021-07-05
            • 1970-01-01
            • 2013-04-14
            • 1970-01-01
            • 1970-01-01
            • 2017-01-12
            • 2019-12-07
            相关资源
            最近更新 更多