We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › replace/exlude char from stringarray
Page Index Toggle Pages: 1
replace/exlude char from stringarray (Read 1359 times)
replace/exlude char from stringarray
May 5th, 2010, 5:17am
 
I have this:

Code:
String[] lines = loadStrings("list.txt");
String[] include = loadStrings("http://www.corriere.it/rss/politica.xml");

ArrayList testoA = new ArrayList();
for( int i = 0; i < lines.length; i++){
//String[] tokens = lines[i].split(" ");
// for (int k = 0; k < tokens.length; k++){
testoA.add(lines[i]);
println(lines[i]);
// }
}

for( int i = 0; i < include.length; i++){
String[] tokens = include[i].split(" ");
for (int k = 0; k < tokens.length; k++){
//println("c'è? " + tokens[k]);
if(testoA.contains(tokens[k])){
println("c'è " + tokens[k])

}
}
}


How I can exclude or replace some chars (like : , . ? ") from the stringarray "include"?
I've tried with
Code:
 include.replace(':', ' ') 



but this doesn't work on array....
Re: replace/exlude char from stringarray
Reply #1 - May 5th, 2010, 5:55am
 
PierG wrote on May 5th, 2010, 5:17am:
<snip>

How I can exclude or replace some chars (like : , . ") from the stringarray "include"
I've tried with
Code:
 include.replace(':', ' ') 



but this doesn't work on array....


Well you can't use replace() directly on the array itself as it's a String method; but you can of course apply it to the individual elements of the array since they are Strings.  So for example this should work:

Code:
for( int i = 0; i < lines.length; i++){
 lines[i].replace(':', ' ');
 // continue with code...

}


Edited:
Sorry - just noticed that strictly speaking you would have to assign the result of the replace() back to lines[i] in the above code:

Code:
for( int i = 0; i < lines.length; i++){
 lines[i] = lines[i].replace(':', ' ');
 // continue with code...

}


Re: replace/exlude char from stringarray
Reply #2 - May 5th, 2010, 2:18pm
 
I did this:

Code:
  for( int i = 0; i < lines.length; i++){
String[] tokens = lines[i].replace(':', ' ').replace(';', ' ').replace('"', ' ').toLowerCase();


I obtain:
"cannot convert string to string[]"  on Mac

but it works on PC.....
Re: replace/exlude char from stringarray
Reply #3 - May 6th, 2010, 1:27am
 
Then you have probably not tested the same code...
The message is explicit, you must declare String instead of String[], because you return an altered line, not multiple tokens (no split there).
Re: replace/exlude char from stringarray
Reply #4 - May 6th, 2010, 3:39am
 
sorry, I've missed the .split(" ")!!!

Code:
 for( int i = 0; i < lines.length; i++){
String[] tokens = lines[i].replace(':', ' ').replace(';', ' ').replace('"', ' ').toLowerCase().split(" ");


ok..... by the way.... There's a way to shorten all the  .replace().replace(). replace()......."?
Re: replace/exlude char from stringarray
Reply #5 - May 6th, 2010, 5:16am
 
Code:
String[] tokens  = lines[i].replaceAll("[:;\"]+", " ").split(" ");  

Page Index Toggle Pages: 1