huabing78

say, string = "abc//123"

I want to replace "//" with "/" so that it becomes "abc/123".

Thanks.



Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

Mark Dawson

Hi,

try:

string s1 = "abc//123".Replace("//", "/");

string s2 = @"abc\\123".Replace(@"\\", @"\");

Note, that if you look at s2 in the debugger you will still see \\ in the debugger window because it escapes the \, but it really only has one \, try printing it to the console to verify.

Mark.






Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

huabing78

yeah, its so tricky, in the debugger, its still "\\". But in the text visualizer, its become "\".

Thanks, Mark. I was pulling my hair out and still couldn't figure it out for one hour..gees





Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

huabing78

sorry, but the problem is still there.

I need pass this string to a sql query paramater. But it still gives errors due to the two "\\". what is happening

I know in text visializer, its "/", but in the runtime of the program, its still "\"

I am going crazy now..help





Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

Mark Dawson

Can you post a quick code snippet and error message of exactly what is going wrong.

Thanks

Mark.






Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

huabing78

_currentUserName = @_currentUserName.Replace(@"\\", @"\");

sqlCommand.Parameters[@"username"].Value = _currentUserName;

_currentUsername still has two slashes....





Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

Mark Dawson

Is it possible that _currentUserName had more than two slashes in it originally i.e. domain\\\username in which case replacing two \\ would still leave you with \\ left. Also did you look in the debugger to verify that the _currentUserName variable is correct before you pass it to the sqlCommand parameter list At what point is the \\ getting reintroduced.

Mark.






Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

Figo Fei - MSFT

Hi,

Say that string str1="abc//"; you want to replace the // with / , just use str1.Replace("//","/"); it returns the result string you need.

If you want the string str2 displays as "abc\ \" changed into "abc\", just use str2.Replace("\\ \\","\ \"); it also returns the result you want.

Note: str2 displays "abc\ \", while it is evaluated as "abc\\ \\". Since \ is known as an escaped character. So, "\" is even not a full string.

Thank you






Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

saumitra.tamrakar

Hi ,

string mystring = "abc abcd abc abcde abc abcdef " ,

so i want to remove "abc" from mystring . if i use

mystring.replace("abc"," ")

mystring will be "d de def " ,

but i want "abcd abcde abcdef "

thanks in advance

saumitra






Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

Figo Fei - MSFT

saumitra.tamrakar wrote:

Hi ,

string mystring = "abc abcd abc abcde abc abcdef " ,

so i want to remove "abc" from mystring . if i use

mystring.replace("abc"," ")

mystring will be "d de def " ,

but i want "abcd abcde abcdef "

thanks in advance

saumitra

The following code can apply to the exact case you described:

string str = "abc abcd abc abcde abc abcdef ";
if (str.StartsWith("abc"))
{
str = str.Substring(3, str.Length-3);
}

Thanks




Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

saumitra.tamrakar

Hi ,

thanks for reply

but my actual problem is

i have to remove common words from a table in c# which have arround 2 lacs records . for that i list out all the words and there occurance in every row . after
i remove words which occurance more than 10 times .
but that all is taking too much time .
how can reduce the time ,








Re: Visual C# General How to use string.replace to replace "\" with "\" in a string?

Figo Fei - MSFT

Hi, saumitra

I think you can take advantage of regular expression in .NET

Since your question is irrelevant to the original question in this thread, If you need further assistance, I suggest you to open a new thread to discuss.

Thanks