473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Replace characters in a string

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
4 14512
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Nicolas Bouillon | last post by:
Hi I would like to replace accentuel chars (like "é", "è" or "à") with non accetued ones ("é" -> "e", "è" -> "e", "à" -> "a"). I have tried string.replace method, but it seems dislike...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
4
by: Prasad S | last post by:
Hello I wish to replace all the characters in a string except those which are inside '<' & '>' characters. And there could be multiple occurences of < & > within the string. e.g. string =...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
3
by: o_swas | last post by:
Hello, I have a JavaScript string. I want to replace all consecutive occurrences of whitespace characters like spaces, tabs, newlines, and form feeds with another string. For example, say I...
9
by: David P. Donahue | last post by:
I'm using RegExp.Replace(string, string, string) to remove some pieces of large strings. But I seem to be having trouble getting it to match "all characters up to and including xxxxx" (which I...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
1
by: coolami4u | last post by:
I need a program that simulates the search-and-replace operation in a text editor. The program is to have only three function calls in main. The first function prompts the user to type a string of...
7
by: Grok | last post by:
I need an elegant way to remove any characters in a string if they are not in an allowed char list. The part cleaning files of the non-allowed characters will run as a service, so no forms here. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.