开发者

Translating vbscript string to c# string

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 & _
              开发者_StackOverflow      "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?


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,""""";


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,\"\"";


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.


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 ;)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜