As I read the question since I have similiar issue about the backslash.
On my webform textbox, I input some thing on like VN\Mary
.
I would like to Split this textbox.Text as 2 words.
the Split worked very well in VB.Net , after I moved to C#, it starts to have problem.
Would anyone give me some advice ?
I even tried textbox.Text.ToCharArray()
but it does not work if I use VN\Thomas
.
I would appreciate your advice.
Thank you.
Asked
Active
Viewed 98 times
-1

Tài Lê Hữu
- 3
- 1
-
You need to escape backslashes in C#. Try `textbo.Text.Split( new Char[] { '\\' } )`. – Dai Oct 03 '22 at 13:37
-
1Uhm, is `VN\Thomas` meant to represent a Windows Active Directory login name? – Dai Oct 03 '22 at 13:37
1 Answers
-1
string[] words = textbox.Text.Split('\\');
EDIT: escape the backslash

peemerdu
- 26
- 2
-
1This doesn't compile: `'\'` is an invalid `char` literal: backslashes must be escaped. – Dai Oct 03 '22 at 13:56
-
hi @peemerdu, I checked, the function is working but it does not work on WebApi when I pass object as `{ "loginID": "a2vn\thomasl", "password": "password", } ` by using the same `Text.Split('\\')` , it does not work. Would you have suggestion ? – Tài Lê Hữu Oct 03 '22 at 14:38
-
@TàiLêHữu - You could do this a few ways which is outside the scope of the original question. Is this in JSON format? I would parse this into an object and run the above solution against the value of the property. – peemerdu Oct 04 '22 at 09:57