views:

219

answers:

5

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?

+1  A: 
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 ;)

pierre
A: 

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.

Bobby
+2  A: 

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,\"\"";
richardtallent
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
Joel Coehoorn
Why can't I type "unescaped" today? I had the same typo in my own answer :(
Joel Coehoorn
+3  A: 

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,""""";
Richard Szalay
+1, but be careful letting extra whitespace slip in this way.
Joel Coehoorn
+1 to your comment :)
Richard Szalay
+2  A: 
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.

Joel Coehoorn
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.
MarkJ
I'm not saying the concatentation would be a bottleneck, but why make it slower than it needs to be?
Joel Coehoorn
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.
Richard Szalay