Replace characters in a string

  • Thread starter Thread starter jpierson
  • Start date Start date
J

jpierson

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,

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
 
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]
 
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
 
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)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top