【问题标题】:Store parts of string in a string array将部分字符串存储在字符串数组中
【发布时间】:2013-06-29 10:20:18
【问题描述】:

我有一个字符串tajmahal.txt sarjan.pdf noorjahan.exe。我想将此字符串存储在一个字符串数组中,例如ar[0] = tajmahal.txtar[1] = sarjan.pdfar[2] = noorjahan.exe。如何在不使用 java 中的任何构建函数的情况下做到这一点(因为我使用的 j2me 不支持许多 j2se 函数)。任何帮助都会很棒。提前致谢。

【问题讨论】:

  • 您想创建自己的自定义函数吗?因为您可以使用 Java 中的 Split 函数来执行此操作。编辑:没关系,我只是看到这个功能在 J2ME 中不可用。
  • 检查这些答案

标签: java arrays string java-me split


【解决方案1】:

因为 String.split 不可用。从这个answer可以看到split的实现@

 public static String[] Split(String splitStr, String delimiter) {
     StringBuffer token = new StringBuffer();
     Vector tokens = new Vector();
     // split
     char[] chars = splitStr.toCharArray();
     for (int i=0; i < chars.length; i++) {
         if (delimiter.indexOf(chars[i]) != -1) {
             // we bumbed into a delimiter
             if (token.length() > 0) {
                 tokens.addElement(token.toString());
                 token.setLength(0);
             }
         } else {
             token.append(chars[i]);
         }
     }
     // don't forget the "tail"...
     if (token.length() > 0) {
         tokens.addElement(token.toString());
     }
     // convert the vector into an array
     String[] splitArray = new String[tokens.size()];
     for (int i=0; i < splitArray.length; i++) {
         splitArray[i] = (String)tokens.elementAt(i);
     }
     return splitArray;
 }

【讨论】:

    【解决方案2】:
        String str="tajmahal.txt sarjan.pdf noorjahan.exe";
        StringTokenizer st=new StringTokenizer(str," ");
        String[] arr=new String[st.countTokens()];
        int i=0;
        while (st.hasMoreElements()){
               arr[i]=st.nextToken();
               i++;
        }
    

    【讨论】:

      【解决方案3】:

      在 J2me 中有 StringTokenizer 的实现。 查看this example 以帮助您完成任务。

      StringTokenizer token;
      token = new StringTokenizer(str);
      int i=0;
      while(token.hasMoreElements()){
      ar[i++]= tok.nextToken();
      }
      

      【讨论】:

        【解决方案4】:

        这取决于您使用的 Java ME 配置/配置文件集。

        说到 CLDC/MIDP,这里没有集合、StringTokenizer、split() 等等。

        1. 先计算那些空格(' ')。
        2. 准备一个字符串[count + 1]。
        3. 在使用 StringBuffer 缓冲字符时拆分每个标记。
        static String[] split(final String string) {
        
            // count spaces
            int spaces = 0;
            for (int i = 0; i < string.length(); i++) {
                if (string.charAt(i) == 0x20) {
                    spaces++;
                }
            }
        
            // prepare the array and buffer
            final String[] split = new String[spaces + 1];
            final StringBuffer buffer = new StringBuffer();
        
            int index = 0;
            for (int i = 0; i < string.length(); i++) {
                if (string.charAt(i) == 0x20) {
                    split[index++] = buffer.toString();
                    buffer.delete(0, buffer.length());
                    continue;
                }
                buffer.append(string.charAt(i));
            }
        
            return split;
        }
        

        【讨论】:

          猜你喜欢
          • 2013-05-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-23
          • 2017-04-08
          • 2016-03-17
          相关资源
          最近更新 更多