views:

59

answers:

3

Is there any tool that will convert a multiline text, to a compatible multiline string for Visual Studio 2008/2005?

For example:

line1
line2
line3
line4

Should become:

"line1" & _
"line2" & _
"line3" & _
"line4"
+1  A: 

Is this what you're looking for?

    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
Tim Schmelter
You don't escape " characters...
Lucero
Thanks Tim, as Lucerno said it does not escape " characters
Yiannis Mpourkelis
+2  A: 

This kind of tool definitely falls in the do-it-yourself category. Start a new Windows Forms application. Paste the code shown below. Put a shortcut to the program on your desktop. To use it, drag a file from Explorer onto the form. Switch to Visual Studio and type 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

The better mousetrap is to add the file as a resource instead of hard-coding the text.

Hans Passant
You could make this a VS add-in also.
Nissan Fan
Thanks Hans for the code
Yiannis Mpourkelis
Your welcome. Six double quotes in a row was a personal first :)
Hans Passant
A: 

A VS Macro for Pasting Long Text as String seems like perfect solution.

Peter Macej