WHat is the C# equivalent of this

kcwallace

Centurion
Joined
May 27, 2004
Messages
175
Location
Austin, TX
Visual Basic:
Dim TmpTxt as String="O'Niel"
TmpTxt.Replace(Chr(39), "'+char(39)+'")

would yield

O'+char(39)+'Niel

I get THis Error:

The name 'Chr' does not exist in the current context
 
In C# that would be spelled:
C#:
string TmpTxt = "O'Niel";
TmpTxt = TmpTxt.Replace('\'', "\+char(39)+'");
// TmpTxt now equals: O'+char(39)+'Niel
Ready for some info? C# specifies character constants by placing a character between single quotes, i.e. apostrophes ('). For example, the X character would be 'X'. Then question mark character would be '?'. Furthermore, certain characters require escape sequences to be represented. For example, an apostrophe can't be included directly into a character literal (just as a double quote can't be directly included in a string literal), so we use the escape sequence \', where the backslash modifies how the following character is interpreted.

To do character constants based on the numerical value of the character you can use a Unicode escape (For example, Chr(39) in VB is the same as '\u0027' in C# [note that 39 in hexadecimal is 27]).

To convert an integer value to its corresponding character at runtime, use the System.Convert.ToChar function.

If you want a more thorough (or clearer) explanation, check out MSDN’s documentation on C# string literals.
 
I need to split a text string into an array. I would normally use the string.split function, however, I cannot seem to make it work. I am using the following code:

C#:
char backSlash = Convert.ToChar("\u005C");
string[] arr=iStr.Split(backSlash);

If you input "x\y" arr has the length of 1 when it should be 2
 
I need to add that I can make it work if I use literals on the input @"x\y", but is there a way that I can use literal strings as paramaters
 
Not sure what you mean by
kcwallace said:
is there a way that I can use literal strings as paramaters

A literal string can be used anywhere a string can be used - it's just a convenience when using strings with \ characters embedded in them as it saves having to double them up.
 
Back
Top