By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,734 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,734 IT Pros & Developers. It's quick & easy.

Replace characters in a string

P: n/a
Hi,

I'm having a few problems with the replace function for replacing
characters in a textbox.
"C:\" is the string i am tryin to remove ,with it a command I am
sending does not work.

txtS_Filename.Text.Replace("C:\", "");

The compiler has a problem with the newline slash character but even
when I take the slash out c: is still not replaced.

I'm using the OpenFileDialog class

txtS_Filename.Text = fdlg.FileName ;

This shouldnt make any difference but maybe it does.

Any help appreciated

James

Mar 25 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
jp******@gmail.com wrote:
Hi,

I'm having a few problems with the replace function for replacing
characters in a textbox.
"C:\" is the string i am tryin to remove ,with it a command I am
sending does not work.

txtS_Filename.Text.Replace("C:\", "");

The compiler has a problem with the newline slash character but even
when I take the slash out c: is still not replaced.

I'm using the OpenFileDialog class

txtS_Filename.Text = fdlg.FileName ;

This shouldnt make any difference but maybe it does.

Any help appreciated

James

Hi James,

Try the following:

txtS_Filename.Text.Replace("C:\\", "");

That should sort out your issues.
The '\' character denotes a escape sequence, so to refer to a literal
'\' character, you have to escape the '\' thus giving you "\\".

I hope this helps

Regards
Materialised
Mar 25 '06 #2

P: n/a
> I'm having a few problems with the replace function for replacing
characters in a textbox.
The compiler has a problem with the newline slash character but even
when I take the slash out c: is still not replaced.


Replace(...) returns the modified string, it does not change it in
place. So, do

txtS_Filename.Text = txtS_Filename.Text.Replace("C:\\", "");

[note also the added backslash; a single backslash appears to the
compiler to be escaping the quote symbol, hence the error you mentioned]

Mar 25 '06 #3

P: n/a
Thanks guys that solves my problem

I was putting txtS_Filename.Text = fdlg.FileName ; after appending the
string instead of before it,so i swapped it round and it works
perfectly.

James

Mar 25 '06 #4

P: n/a
You do realize that there is a Path class in the .NET Framework for
manipulating file paths? Perhaps this can do what you want without, for
example, coding into your application that something is on drive C (and
capital C and not small c, etc)?

Mar 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.