是否有任何工具可以转换Visual Studio 2008/2005的多行文字?

时间:2010-05-24 20:08:34

标签: vb.net visual-studio-2008 visual-studio-2005 string

是否有任何工具可以将多行文本转换为Visual Studio 2008/2005的兼容多行字符串?

例如:

line1
line2
line3
line4

应该成为:

"line1" & _
"line2" & _
"line3" & _
"line4"

3 个答案:

答案 0 :(得分:2)

这种工具绝对属于自己动手的类别。启动一个新的Windows窗体应用程序。粘贴下面显示的代码。在桌面上放置程序的快捷方式。要使用它,请将文件从资源管理器拖到窗体上。切换到Visual Studio并键入Ctrl + V.

Public Class Form1
    Public Sub New()
        InitializeComponent()
        Me.AllowDrop = True
    End Sub

    Protected Overrides Sub OnDragEnter(ByVal e As DragEventArgs)
        If e.Data.GetDataPresent("FileDrop") Then e.Effect = DragDropEffects.Copy
    End Sub

    Protected Overrides Sub OnDragDrop(ByVal e As DragEventArgs)
        Dim files = DirectCast(e.Data.GetData("FileDrop", False), String())
        Dim txt As New System.Text.StringBuilder
        Dim lines = System.IO.File.ReadAllLines(files(0))
        For ix As Integer = 0 To lines.Length - 1
            txt.Append("""" + lines(ix).Replace("""", """""") + """")
            If ix < lines.Length - 1 Then txt.AppendLine(" & _")
        Next
        Clipboard.SetText(txt.ToString())
    End Sub
End Class

更好的捕鼠器是将文件添加为资源,而不是对文本进行硬编码。

答案 1 :(得分:1)

这是你要找的吗?

    Dim testString As String = "line1" & vbCrLf & _
                               "line2" & vbCrLf & _
                               "line3" & vbCrLf & _
                               "line4"
    Dim allLines() As String = Microsoft.VisualBasic.Strings.Split(testString, vbCrLf)
    Dim strConverter As New System.Text.StringBuilder
    For Each line As String In allLines
        strConverter.Append("""" & line & """").Append(" & _").Append(vbCrLf)
    Next
    If allLines.Length > 0 Then strConverter.Length -= (" & _" & vbCrLf).Length
    Dim convertedString As String = strConverter.ToString

答案 2 :(得分:0)

VS Macro for Pasting Long Text as String似乎是完美的解决方案。