簡體   English   中英

如何在保留不同分隔符計數的同時拆分字符串?

[英]How to split a string while keeping a count of the different delimiters?

因此,輸入看起來非常像{q1,q2},{a,b},{[q1:a:q2],[q2:b:q2]},q1,{q2} DFA。

我想做的是用逗號,方括號和冒號將其分開。 然后分別打印出結果(即大括號中的所有內容都有其自己的方法)。

例:

第1部分 = q1 q2

第2部分 = Ab

Part3 = q1通過

q2用b轉到q2

第4部分 = q1

第5部分 = q2

我當時想的是保持大括號的計數,如果大括號的計數= 1、3、5等,它們將相應地執行這些方法。

問題是,當我將其用作字符串時,我無法確保它將“ q1”視為一個字符串,而不是“ q”“ 1”

當我使用.split(\\ s *,\\ s * | \\ {| \\} | \\ [| \\])分割字符串時,這些字符將被刪除 ,我無法再繼續對其進行計數。

或者我應該只保留花括號,打印一個子字符串(取下花括號),一旦看到一個緊密的花括號,它將移至下一個方法。

我試過的

分割字符串並存儲到列表中

List<String> listDFA = Arrays.asList(DFA.split("\\s*,\\s*|\\{|\\}|\\[|\\]"));

啟動DFA

for (index = 0; index<size; index++){
     if (curlyBrackets.contains(listDFA.get(index))){ //curlyBrackets has "{}"
        System.out.println("curly"); // just a test if it sees a curly will omit later
     }

     System.out.println(index); // again a test wanted to see what was being indexed
     System.out.println(listDFA.get(index));
  }

我想嘗試的是:

for (index = 0; index <size; index++){
     if (curlyBrackets.contains(DFA.substring(index, index+1))){
        curly++;
        if (curly == 1){
           index++;
           states(DFA);
        }
     }
}

States()是:

public static void states (String DFA){
  //List<String> stateQ = new ArrayList<String>(Arrays.asList(DFA.split(" , "))); // I tried creating the list here, but then there would be a couple of incosistent variable such as index and size.
  lineVector = DFA.split(",");
  int size = lineVector.length;
  while(lineVector(index) != '}'){
     System.out.println(stateQ[index]); //DFA.charAt(index) lineVector[index] was trying either or...
     index++;
  }
  curly++;

我猜該問題有某種熱修復,但是我仍然使用逗號作為分隔符(而不是括號)來分割字符串。

List<String> listDFA = Arrays.asList(DFA.split("\\s*,\\s*"));

因此,使字符串的每個內容都與一個索引相關聯(括號被附加到最近的非逗號)。 然后將該索引存儲到字符串中。

String currString = listDFA.get(index);

然后查看該字符串的任何部分是否有大括號

if (currString.indexOf('{') != -1 || currString.indexOf('}') != -1 )

並匹配大括號條件后,執行該塊

if (curly <= 2){
           Q.add(currString);
     }

現在我需要弄清楚的是如何使用方括號執行此操作,因為我必須將其存儲到2D數組中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM