When I read up the contents in a text file ... How can I remove certain char that I dont want it to be display when I print out the contents from the text file ?
e.g. the content in the file is
Name : XXXXXX
Age : 19
I want to remove the "Name :" and "Age :" , so that XXXXXX and 19 will be display ..
9 replies to this topic
#1
Posted 17 April 2011 - 07:57 AM
|
|
|
#2
Posted 17 April 2011 - 08:17 AM
Like so:
String line1 = "Name : XXXXXX";
String line2 = "Age : 19";
line1 = line1.replaceAll("^Name : ", "");
line2 = line2.replaceAll("^Age : ", "");
System.out.println(line1);
System.out.println(line2);
Or:
String line3 = "Birthdate : 19/09/1999";
line3 = line3.substring(line3.indexOf(":")+2);
#3
Posted 17 April 2011 - 08:22 AM
what if there's many file name in the text content... How can I remove all the file name fromthe content?
#4
Posted 17 April 2011 - 08:43 AM
Anyone can help ?
#5
Posted 17 April 2011 - 08:46 AM
You do it line by line, using wim DC's suggestion.
#6
Posted 17 April 2011 - 08:55 AM
WingedPanther said:
You do it line by line, using wim DC's suggestion.
There's thouands of filename ... and you want me to do it line by line ? ... It will take hours for me to do that ... LOL ?
#7
Posted 17 April 2011 - 09:31 AM
A loop, home slice.
ArrayList<String> strs = new ArrayList<String>();
for (String extractValueFrom : userInputLines)
{
strs.add(extractValueFrom.substring(extractValueFrom.indexOf(":") + 2));
}
Now the extracted values are in the ArrayList strs. You can display those using an equivalent loop with System.out.println(strs.get(i)).
Wow I changed my sig!
#8
Posted 17 April 2011 - 09:40 AM
ZekeDragon said:
A loop, home slice.
ArrayList<String> strs = new ArrayList<String>();
for (String extractValueFrom : userInputLines)
{
strs.add(extractValueFrom.substring(extractValueFrom.indexOf(":") + 2));
}
Now the extracted values are in the ArrayList strs. You can display those using an equivalent loop with System.out.println(strs.get(i)).sorry I dont get what you are tryong to say ...
here's my code :
FileInputStream file = null;
try {
file = new FileInputStream("C:\\Users\\Johnny\\Downloads\\Desktop\\TheFile.csv");
BufferedReader br = new BufferedReader(new InputStreamReader(file));
String strLine = null;
while ((strLine = br.readLine()) != null) {
String results = strLine.replaceAll(".mol2", "").replace(",", "");
if (strLine.trim().contains("Name")) {
continue;
}
System.out.println(results);
}
} catch (IOException e) {
e.printStackTrace(); // Alternate way to print the entire error
} finally {
// Close file
try {
if (file != null) {
file.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
all files names all contain a .mol2 extension .. How can I modify it with your code ? Sorry I anot very good in java :(
#9
Posted 17 April 2011 - 09:59 AM
Just add
results = results.substring(results.indexOf(":") + 2)
in the while loop I guess.
#10
Posted 17 April 2011 - 07:12 PM
wim DC said:
Just add
results = results.substring(results.indexOf(":") + 2)
in the while loop I guess.this is the exactly ans i'm looking for ... thx ;)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









