转换格式化文本的字符串,有更好的方法吗?

时间:2014-07-16 02:56:04

标签: java

这是我正在解析的文本文件

  

//(*       设置表格限制最小+颜色代码 *
      1,3
      2,3
      5,0
      10,5
      15,6
      25,8
      50,7
      100,10
      *设置表格限制最大*
      100个
      200个
      300个
      500个
      1000
      2000
      3000
      5000
      10000个
      <>

这是代码

private void addMinBets(String aDataRow) {

    // Process Min limits
    // Does aDataRow start with "!= *"
    // process string
    // else do nothing
    // Process Max limits
   if (!aDataRow.contains("*")){
            // Find the index of ","
             // Get string before ","
             // Get string After ","
       if (aDataRow.contains(",")){
                 // Add Max bet to maxArraylist
           String parts[] = aDataRow.    split("\\,");
           Log.d("Split"," Match must be min =  $" + parts[0]);
           Log.d("Split"," Match must be min color = " + parts[1]);
       }
       if (!aDataRow.contains("*")){

           if (!aDataRow.contains(",")){

               if (!aDataRow.contains("<")){

                 Log.d("Split"," Match must be max bet = $ " + aDataRow);

               }


           }

       }

   }

在代码中有更有效的方法吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

在我看来,您只想记录以下形式的字符串:

  1. 位(S)
  2. 位(S),数字(S)
  3. 然后你想使用@Thusitha Thilina Dayaratne建议的正则表达式。

    使用link作为示例制作快速示例。

    private void addMinBetsV2(String aDataRow) {
        //[one or more digits]
        String numRegex = "(\\d)+"; 
        //[one or more digits],[one or more digits]
        String commaNumRegex = numRegex + "," + numRegex;
    
        if (aDataRow.matches(numRegex)) {
            Log.d("Split"," Match must be max bet = $ " + aDataRow);
        } else if (aDataRow.matches(commaNumRegex)) {
            String parts[] = aDataRow.    split("\\,");
            Log.d("Split"," Match must be min =  $" + parts[0]);
            Log.d("Split"," Match must be min color = " + parts[1]);
        }
    }