i have been looking through the different questions as to “how to remove spaces” from various stuff, and i get the basics of it, the problem is that i cant find a way to get it done in my code, as it is a bit tricky to “just” add a function to trim the spaces away.
public static ArrayList<String> searchForSong(String path) {
ArrayList<String> matchingFiles = new ArrayList<String>();
File folder = new File(path);
File[] files = folder.listFiles();
for (File f : files) {
if (f.isFile()) {
try {
FileInputStream fis = new FileInputStream(
f.getAbsolutePath()); // laver en stream baseret på
// en fil
int b; // til at putte bytes i
StringBuilder fileCont = new StringBuilder(); // for at
// sætte
// bytes'ne
// sammen
// til en
// string
while ((b = fis.read()) != -1) // så længe den ikke er -1
// (EOF).. læs!
{
fileCont.append((char) b);// put byten i stringbuilderen
}
// System.out.println(fileCont.toString()); //skriv
// string'en/filens indhold ud..
if (fileCont.toString().toLowerCase().contains(jTextFieldSearch.getText().toLowerCase())) {
matchingFiles.add(f.getAbsolutePath());
}
} catch (FileNotFoundException e) { // skal på, fordi
// FileInputStream smider
// FileNotFoundException
e.printStackTrace();
} catch (IOException e) { // skal på, fordi fis.read() smider
// IOException
e.printStackTrace();
}
}
}
return matchingFiles;
}
In this code i would like two strings to have removed their spaces the one called fileCont.toString() and jTextFieldSearch.getText
Anyone who can help? it would be really great!
Change your if statment like this…