Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 9th, 2007, 12:58 AM
Registered User
 
Join Date: Nov 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Replce Problem

How to replace "\" with "\\"
i am using str.replace("\","\\") but its not working


 
Old January 9th, 2007, 01:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

The character '\' is used as an escape character.
So when you include it in a string the next char is the one that is considered what you are after.
So str.replace("\","\\") means if you find 'nothing/empty string' replace it with one '\'.
You need something like the following
str.replace("\\","\\\\")
or
str.replace(@"\",@"\\")
I think the second is the better approach

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old January 9th, 2007, 02:33 AM
Authorized User
 
Join Date: Nov 2006
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

your may use.
str=str.Replace("\","\\");
because str.Replace only makes virtual changes.
not stored to your str variable.Ok
I Think This one help you.

Thanks and Regards
Kumar Ashish
9350789189
 
Old January 9th, 2007, 03:39 AM
Registered User
 
Join Date: Nov 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by rodmcleay
 The character '\' is used as an escape character.
So when you include it in a string the next char is the one that is considered what you are after.
So str.replace("\","\\") means if you find 'nothing/empty string' replace it with one '\'.
You need something like the following
str.replace("\\","\\\\")
or
str.replace(@"\",@"\\")
I think the second is the better approach

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================

thank you
now my problem get solved by your solution
tnaks a lot











Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.