0

So i have this code

sb.AppendLine(  "var nameSplit = users[i].split('\');");

The sb.AppendLine is C# and the bit inside is JavaScript, I am trying to split users[i] so that I can strip out the Domain name, unfortunately this does not work I have also tried

sb.AppendLine(  "var nameSplit = users[i].split('\\');");

It doesn't make much sense really, I have done an alert of nameSplit and it shoes that it splits the text on every letter as though the \ escapes nothing, the second example doesnt work at all when I would expect that to and as javascript on its own it works fine.

Any help appreciated.

Thanks

Matt

2 Answers 2

3

Try

sb.AppendLine(  "var nameSplit = users[i].split('\\\\');");

this will give the javascript

var nameSplit = users[i].split('\\');

\ is an escape character in both C# and javascript so you need two of then in a litteral to specify that you need one in the real string.

2
  • Per I think I love you, genuinely haha.
    – Mattmoo
    Commented May 14, 2012 at 20:10
  • P.s. yes it worked :D
    – Mattmoo
    Commented May 14, 2012 at 20:11
1

Try adding a '@' in front of the quote, so it would look like....

sb.AppendLine(  @"var nameSplit = users[i].split('\');");

The @ symbol should make the code take the string 'as is' and ignore special characters and whatnot.

2
  • Woopsy, Per Jakobsen is right, the above would need to be '\\' with the @, or '\\\\' without
    – Zork
    Commented May 14, 2012 at 20:02
  • Thanks Zork, I didn't try this one as Per's worked but I appreciate you taking the time to answer :) Matt
    – Mattmoo
    Commented May 14, 2012 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.