-1

例如,我有这样的字符串

309\306\308\337_338

309\306\337_338

310

311\315_316\336_337

311\315_316\336_337

311\335_336

这些字符串表示页码列表,例如字符串“309\306\308\337_339”表示第 309,306,308,337,338,339 页

我想将这些字符串之一传递给函数,该函数将其作为字符串返回,如 309,306,308,337,338,339

这个函数可以做到这一点,但在 c# 中,我想在 android 中刺穿

 private static string Get_PageNumbers(string str)
    {
        ArrayList arrAll = new ArrayList();
        MatchCollection match;
        string[] excar;
        string strid, firstNumber, lastlNumber;
        int fn, ln;
        ArrayList arrID = new ArrayList();

        //***In Case The Range Number Between "_"
        if (str.Contains("_"))
        {
            // match_reg = new Regex("(w?[\\d]+)*(_[\\d]+)");
            Regex matchReg = new Regex("(w?[\\69]+_[\\d]+)*(q?[\\d]+//)*(a?[\\d]+_[\\d]+)*(y?[\\d]+)*");
            match = matchReg.Matches(str);

            int count = match.Count;
            excar = new string[0];
            for (int i = 0; i < count; i++)
            {
                Array.Resize(ref excar, count);

                excar[i] = match[i].Groups[0].Value;
                if (excar[i] != string.Empty)
                    arrID.Add(excar[i]);
            }

            //******IF Array Contains Range Of Number Like"102_110"
            if (str.Contains("_"))
            {
                for (int i = 0; i < arrID.Count; i++)
                {
                    strid = arrID[i].ToString();

                    if (arrID[i].ToString().Contains("_"))
                    {
                        int idy = strid.LastIndexOf("_");
                        firstNumber = strid.Substring(0, idy);

                        if (idy != -1)
                        {
                            lastlNumber = strid.Substring(idy + 1);
                            fn = int.Parse(firstNumber);
                            arrAll.Add(fn);
                            ln = int.Parse(lastlNumber);
                            for (int c = fn; c < ln; c++)
                            {

                                fn++;
                                arrAll.Add(fn);

                            }
                        }
                    }
                    else
                    {
                        arrAll.Add(arrID[i].ToString());
                    }
                }

                //******If Array Contain More Than One Number
                if (arrAll.Count > 0)
                {
                    str = "";
                    for (int i = 0; i < arrAll.Count; i++)
                    {

                        if (str != string.Empty)
                            str = str + "," + arrAll[i];
                        else
                            str = arrAll[i].ToString();
                    }

                }
            }
        }

        //***If string Contains between "/" only without "_"

        else if (str.Contains("/") && !str.Contains("_"))
        {
            str = str.Replace("/", ",");
        }
        else if (str.Contains("\\"))
        {
            str = str.Replace("\\", ",");
        }
        return str;
    }
4

2 回答 2

1

我认为这更容易用split函数来完成:

public static String Get_PageNumbers(String str) {// Assume str = "309\\306\\308\\337_338"
    String result = "";
    String[] pages = str.split("\\\\"); // now we have pages = {"309","306","308","337_338"}
    for (int i = 0; i < pages.length; i++) {
        String page = pages[i];
        int index = page.indexOf('_');
        if (index != -1) { // special case i.e. "337_338", index = 3
            int start = Integer.parseInt(page.substring(0, index)); // start = 337
            int end = Integer.parseInt(page.substring(index + 1)); // end = 338
            for (int j = start; j <= end; j++) {
                result += String.valueOf(j);
                if (j != end) { // don't add ',' after last one
                    result += ",";
                }
            }
        } else { // regular case i.e. "309","306","308"
            result += page;
        }
        if (i != (pages.length-1)) { // don't add ',' after last one
         result += ",";
        }
    }
    return result; // result = "309,306,308,337,338"
}

例如这个函数调用时如下:

String result1 = Get_PageNumbers("309\\306\\308\\337_338");
String result2 = Get_PageNumbers("311\\315_316\\336_337");
String result3 = Get_PageNumbers("310");

回报:

309,306,308,337,338
311,315,316,336,337
310
于 2012-08-02T15:17:47.160 回答
0

如果我可以建议不同的实施....

  1. 首先,用 "\" 分割字符串str.split("\\");,在这里你会收到一个带有单个数字或类似 "num_num" 的模式的数组字符串
  2. 对于所有创建的字符串,如果字符串不包含“ ”字符,则将字符串放入另一个数组(othArr 命名),然后用“ ”再次拆分str.split("_");,现在您有一个 2 位置数组
  3. 将这2个字符串转换为整数
  4. 现在创建一个战利品以 min val 形式 max val 或两个转换的字符串(并将其放入 othArr)
  5. 将 othArr 转换为用“,”分隔的字符串
于 2012-08-02T15:26:12.363 回答