[英]problems with splitting line using tab delimiter
我正在從以下格式的文件中讀取輸入:
a b c d e
其中每個字母代表一個字符串,並由tab
符分隔。 我正在使用bufferedreader讀取行並使用以下方法進行拆分:
String[] splitString= currentLine.split("\t");
其中currentLine
是一個字符串,其中包含bufferedReader讀取的行。
但是,在輸入中可能存在某些實例,其中b
可以為空白或e
可以為空白(請參見上面的示例輸入)。
我想要做的是對這些實例分別執行以下操作:
if (b is blank){
do this
}else if (e is blank) {
do this
}else{
do this
}
但是我無法提出合適的條件來准確實現此功能
歡迎任何建議
編輯:
只要沒有“空白”,這就是輸入的正常形式
a[tab]b[tab]c[tab]d[tab]e
假設現在應該放在b
位置的東西都是空白,輸入看起來像這樣:
a[tab][tab]c[tab]d[tab]e
您可以嘗試:
String[] splitString = currentLine.split("\\s");
正則表達式\\\\s
匹配單個空格字符。
空格字符由[ \\t\\n\\x0B\\f\\r]
。
根據選項卡拆分(使用\\\\t
表示僅拆分選項卡)。 空字符串將表明您連續有兩個選項卡。
string.split("\\t");
例:
String str = "hello[tab]hi[tab]how are you?"
String[] arr = str.split("\\t");
將得到一個大小為3的元素數組: hello, hi, how are you?
但是,對於String str = "hello[tab][tab]hi[tab][tab][tab]how are you?"
,結果將是一個大小為6的數組,其中包含元素hello, , hi, , , how are you?"
只需檢查您的“空字符串”在哪里,它們就會告訴您哪里缺少什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.