搜索單個字符的字符串的ArrayList
的正確語法是什麼?我想檢查數組中的每個字符串的單個字符。 最終我想執行多個搜索並基於字符串中單個字符的存在替換數組中的所有字符串。 我已閱讀java-examples.com和java文檔以及搜索ArrayLists的幾種方法。他們中沒有一個完成我所需要的。 P.S.使用某種文件庫來執行多個搜索和替換的任何指針都會很好。在字符串中搜索某個字符的ArrayList
---編輯---
按MightyPork的建議arraylist
修訂,使用簡單的string
型。這也使它與包含的hoosssein解決方案兼容。
public void ArrayInput()
{
String FileName; // set file variable
FileName = fileName.getText(); // get file name
ArrayList<String> fileContents = new ArrayList<String>(); // create arraylist
try {
BufferedReader reader = new BufferedReader(new FileReader(FileName)); // create reader
String line = null;
while ((line = reader.readLine()) != null) {
if(line.length() > 0){ // don't include blank lines
line = line.trim(); // remove whitespaces
fileContents.add(line); // add to array
}
}
for (String row : fileContents) {
System.out.println(row); // print array to cmd
}
String oldstr;
String newstr;
oldstr = "}";
newstr = "!!!!!";
for(int i=0;i<fileContents.size();i++){
if(fileContents.contains(oldstr)){
fileContents.set(i, fileContents.get(i).replace(oldstr, newstr));
}
}
for (String row : fileContents) {
System.out.println(row); // print array to cmd
}
// close file
}
catch (IOException ex) { // E.H. for try
JOptionPane.showMessageDialog(null, "File not found. Check name and directory.");
}
}
您需要手動遍歷列表,並執行搜索和替換。 –
如果你能告訴我們你到目前爲止做了什麼,那將是非常棒的。 – gumenimeda
@ gumenimeda,完成。 – InDeep