我有一個平面文件,我試圖擦除導入數據庫。佈局不一致,但始終由一個字段名(可變長度文本,但始終爲9個單詞或短語之一),後跟一個自由格式文本字段(可變長度至1024字節)。我需要提取1024字節的字段,並將它們排列爲跨字段名的列。在變量上使用多個String方法的正確方法是什麼?
輸入文件:
foo-01 bunches of data
foo bar01 more bunches of data including a bunch of notes
foo-01 lots of data lives in this field
foo18 monday notes
...etc.
輸出文件 - 分隔,空間修剪
foo-01;foo bar 01;foo18 (<-- header row)
bunches of data; more bunches of data including a bunch of notes; ;
lots of data lives in this field; ; notes
我的策略是這樣的:閱讀每一行。如果行從九個字段名稱之一開始,則我將一個子字符串(字段名稱後面的第一個字符,通過行中的最後一個字符 - 已修剪的空格)寫入適當列位置中的已刪除平面文件。
此代碼的工作,
if(inputLine.startsWith("foo-01"))
{
String lineVal = inputLine.trim();
int lVLen = lineVal.length();
String outVal = lineVal.substring(17,lVLen);
String outValTrim = outVal.trim();
System.out.println(evalVal+" "+inputLine+" "+outValTrim);
}
else
...etc...
但提出了一些問題。
考慮:
String outValTrim = inputLine.trim().substring(17,inputLine.trim().length()).trim();
什麼的,我可以用方法的最大數量?例如
foo = Stringmethod1.Stringmethod2.StringMethod3()
對聲明中方法的順序是否有規則?
在一個語句中組合方法的最佳做法是什麼?我覺得它不太適合人類閱讀,而且我也不確定效率。
彼得很好地回答了你的直接問題。另外,您可以使用split命令更明顯地分割數據字段。例如。 String headerAndData [] = inputLine.trim()。split(「」,1); – phatfingers