Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Per I think I love you, genuinely haha. –  Mattmoo May 14 '12 at 20:10
    
P.s. yes it worked :D –  Mattmoo May 14 '12 at 20:11

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.

share|improve this answer
    
Woopsy, Per Jakobsen is right, the above would need to be '\\' with the @, or '\\\\' without –  Zork May 14 '12 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 May 14 '12 at 20:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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