【问题标题】:Tokens - Strings Java令牌 - 字符串 Java
【发布时间】:2018-11-05 15:48:06
【问题描述】:

我成功完成了以下任务,但是代码效率非常低,如果有人能告诉我一个更有效的方法,我将不胜感激,也许使用子字符串。作业:

/**
 * Separates a given string into tokens, which are the "words" that are
 * separated by one or more occurrences of the given separator character.
 * Returns the tokens as an array of String values.
 */
public static String[] tokenize (String str, char separator) {
    // Removes all the occurrences of the separator at the beginning and end of str
    String source = trim(str, separator);
    String[] tokens = new String[charRunCount (source,separator)+1];
    String tmp = ""; // a string in order to take a word, then run over this string
    int j = 0;
    int i = 0;
    while (i < tokens.length) {
        if ( source.charAt (j) != separator ) {
            do {
                tmp += source.charAt (j);
                if ( j >= source.length () - 1 ) {
                    break;
                }
                else { // so that we math the source length
                    j++;
                }
            } while (source.charAt (j) != separator); 
        }
        if ( source.charAt (j) == separator ) {
            j++;
            while (source.charAt (j) == separator) {
                j++;
            }
        }
        tokens[i] = tmp;// taking the token into place
        tmp = ""; //resetting the token so we can begin anew

        i++;
    }
    return tokens;
}

cahrRunCount 函数:

    public static int charRunCount(String str, char c){
    char last = 0;
    int counter = 0;
    for (int i = 0; i < str.length(); i++) {
        // whenever a run starts.
        if (last != c && str.charAt(i) == c) {
            counter++;
        }
        last = str.charAt(i);
    }
    return counter;
}

我不能使用 import 或 regex,谢谢!

【问题讨论】:

  • 如果您的代码有效但需要改进,CodeReview 可能是一个更好的发布位置。
  • 对不起,我不知道有这样的网站存在

标签: java string token


【解决方案1】:

使用 String.split 方法

String[] tokens = str.split(separator)


for(String token:tokens){
//your code goes here
}

文档在这里

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

【讨论】:

  • 例如,您将如何在 for 循环中使用它?因为分隔符是一个字符
【解决方案2】:

如果需要,可以使用 String 类的 split 方法(就像@Amidala Siva Kumar 建议的那样),像这样:

public static String[] tokenize (String str, char separator) {
    String[] tokens = str.split(separator);
    return tokens;
}

或者,如果您想使用自己的拆分来执行此操作,您可以这样做(对您的代码的改进)。

public static String[] tokenize (String str, char separator) {
    String sep = ""+separator;
    int max_size = str.length() - str.replace(sep, "").length() +1 ; // Calculate max array size
    String[] temp = new String[max_size];
    int start = 0, index = 0, exact_size = 0;
    int pos = str.indexOf(separator);
    while (pos>=start) {
        if (pos>start){
            temp[index++] = str.substring(start,pos).trim();
            exact_size++;
        }
        start = pos + 1;
        pos = str.indexOf(separator,start); 
    }
    String[] tokens = new String[exact_size];
    System.arraycopy(temp, 0, tokens, 0, exact_size); 
    return tokens;
}

希望你觉得它有用。

【讨论】:

  • 我不知道拆分功能,您将如何实现它?
猜你喜欢
  • 2015-10-09
  • 1970-01-01
  • 2011-11-02
  • 2014-12-04
  • 1970-01-01
  • 2021-03-03
  • 2017-08-19
  • 2013-07-08
  • 1970-01-01
相关资源
最近更新 更多