0

Ok I have a vbscript string in a wsf file as below:

 Dim strTransData = "1,1,2,123457,1,20051012093000" & vbCrLf & _
                    "2,1" & vbCrLf & _
                    "2,2" & vbCrLf & _
                    "2,3" & vbCrLf & _
                    "3,""5449000000996"",0,3,39" & vbCrLf & _
                    "3,"""",100,1,500" & vbCrLf & _
                    "4,0,200,"""""

What I need to do is convert this string to a c# string, I understand that a vbscript & translates to a + in c# string concatentation but what does vbCrLf and _ translate to?

5 Answers 5

3

Alternatively, you can use a verbatim string literal, in which case your quote encoding would remain the same and your newlines are actual newlines:

string transData = @"1,1,2,123457,1,20051012093000
2,1
2,2
2,3
3,""5449000000996"",0,3,39
3,"""",100,1,500
4,0,200,""""";
Sign up to request clarification or add additional context in comments.

1 Comment

+1, but be careful letting extra whitespace slip in this way.
2

C# uses:

  • "+" for concatenation
  • No line-continuation characters (the "_" in VB.NET)
  • \" for a literal quote (rather than "" like VB.NET)
  • \r\n for a CR and LF (literally, inside a string)
  • Alternately, you can use Environment.NewLine

So:

 string strTransData = "1,1,2,123457,1,20051012093000" + "\r\n" + 
     "2,1" + "\r\n" + 
     "2,2" + "\r\n" + 
     "2,3" + "\r\n" + 
     "3,\"5449000000996\",0,3,39" + "\r\n" + 
     "3,\"\",100,1,500" + "\r\n" + 
     "4,0,200,\"\"";

2 Comments

Actually, it's probably easier to translate just using unescapted strings (precede the inital quote for the literal with a "@" character ) and you should avoid \n and \r in favor or Environment.Newline
Why can't I type "unescaped" today? I had the same typo in my own answer :(
2
string TransData = new StringBuilder("1,1,2,123457,1,20051012093000",100).AppendLine("")
         .AppendLine("2,1")
         .AppendLine("2,2")
         .AppendLine("2,3")
         .AppendLine(@"3,""5449000000996"",0,3,39")
         .AppendLine(@"3,"""",100,1,500")
         .Append(@"4,0,200,""""")
         .ToString();

This will avoid any string concatenation (which can be slow in .Net) and allocates a buffer up front that can hold the entire result string. Note the use of un-escaped strings to make converting the escaped quotes easier.

3 Comments

If the string concatenation was fast enough in the original VBScript, it probably is in .Net! I suppose the VBScript may have been a prototype though.
I'm not saying the concatentation would be a bottleneck, but why make it slower than it needs to be?
Using StringBuilder for this example is slower than richardtallent's solution since his will be joined into a single string at compile-time. Using Environment.NewLine, however, would be slower since it needs to be resolved at runtime. Even so, I'd probably recommend String.Concat over a StringBuilder for that scenario.
1
Environment.NewLine

Or use something like http://www.developerfusion.com/tools/convert/vb-to-csharp/ if you have lots to convert.

EDIT: That link appears dead for now.. there are other free online converters. But for something so simple as a string like this; you can do it manually ;)

Comments

0

vbCrLf is a synonym for a line-end (Environment.NewLine).

_ (underscore) allows multiple line statements within VB.

Also, double-double-quotes "" are the VB way of escaping the quotes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.