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 › String replace char
Page Index Toggle Pages: 1
String replace char (Read 2296 times)
String replace char
May 9th, 2008, 7:35pm
 
Hi,
I'm getting

  unexpected token:char

with line 2 of

String thisString = "a_jpeg_file.jpg";
thisString.replace(char ".", char "_");
print(thisString)

so I must have the syntax wrong.  
Anybody know how to do this?

(Actually, I would ideally like to remove the last four characters
but I'm also having trouble accessing the characters of the string
so I can pop out the last four....)
Re: String replace char
Reply #1 - May 9th, 2008, 7:40pm
 
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replace(char,%20char)

String s = "mesquite in your cellar";
s.replace('e', 'o');

returns "mosquito in your collar"

(you don't need the 'char's)
Re: String replace char
Reply #2 - May 9th, 2008, 7:41pm
 
(that's messed up the url, you'll need to cut and paste it. or click and scroll down)
Re: String replace char
Reply #3 - May 9th, 2008, 8:01pm
 
thanks...
you don't by any chance know how to get rid of the last four characters do you?

something like

x= thisString.length;

thisString[x].remove

??  
(thanks!)
Re: String replace char
Reply #4 - May 10th, 2008, 5:58pm
 
From the first example, I believe you want to remove the file extension.
You should do it "smartly": some files have no extension, others have 1, 2, 4 or more letter extensions (.c, .pl, .jpeg or .html, etc.), others start with a dot (.htaccess).
So you better find the position of the last dot of the file and remove from there, if beyond first position.
Code:
String thisString = "a_jpeg_file.jpg";
String name = thisString;
int dotPos = thisString.lastIndexOf(".");
if (dotPos > 0)
name = thisString.substring(0, dotPos);
print(name);
Page Index Toggle Pages: 1