VS 宏/插件将字符串连接转换为 string.format 样式

发布于 2024-09-11 23:24:09 字数 238 浏览 1 评论 0原文

我有一个正在开发的项目,其中很多地方都使用了诸如“Hi”+variable+“,welcome to Project”之类的字符串操作(给出的示例非常小)。

要求之一是将其转换为 string.format 样式。

这是一项非常漫长且乏味的工作,我不想破坏早期的工作代码,因为在转换它时可能会发生任何人为错误。

我想知道我是否可以创建任何宏或 VS 命令来处理它。就像我们标记代码块并在重构选项中执行提取功能一样。

I have project in development where string operations like "Hi " + variable + ", welcome to Project" are used at many places (given example is very minor one).

One of the requirement is to convert it to string.format style.

It is very long and tedious job, where I would not like to break earlier working code due to any human error might happen while converting it.

I would like to if any Macro or VS command which I can create to handle it. Just like we mark block of code and do Extract function in Re-factor options.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2024-09-18 23:24:09

我觉得在这里发布代码有点长,但我在我的博客上发布了答案:
http://www.brianschmitt.com/2010/08/ conversion-concatenated-string-into.html

-- 编辑 --
这里的每条评论都是相关的宏 - 不知道为什么你无法访问......

Public Sub ConvertToStringFormat()
    DTE.UndoContext.Open("ConvertToStringFormat")
    Dim textSelection As TextSelection = DTE.ActiveDocument.Selection
    Dim output As String = "string.Format(""{0}"", {1})"
    Dim delimt As String = ", "
    Dim fmtdTostring As String = ".tostring("""

    Dim txtSelection As String() = System.Text.RegularExpressions.Regex.Split(textSelection.Text.Trim, "\+\s_[+\n\r\t]|&\s_[+\n\r\t]|\+|&")
    Dim hardStrings As String = String.Empty
    Dim valueStrings As String = String.Empty
    Dim counter As Int16 = 0

    For Each str As String In txtSelection
        Dim tmpString As String = str.Trim
        If tmpString.StartsWith("""") Then
            hardStrings &= tmpString.Substring(1, tmpString.Length - 2)
        Else
            Dim fmt As String = String.Empty
            Dim indxToString As Int32 = 0

            If tmpString.ToLower.Contains(fmtdTostring) Then
                indxToString = tmpString.ToLower.IndexOf(fmtdTostring)
                fmt = tmpString.Substring(indxToString + 11, tmpString.Length - tmpString.ToLower.IndexOf(""")", indxToString) - 1)
            End If

            If fmt <> String.Empty Then
                hardStrings &= "{" & counter.ToString & ":" & fmt & "}"
                valueStrings &= tmpString.Substring(0, indxToString) & delimt
            Else
                hardStrings &= "{" & counter.ToString & "}"
                valueStrings &= tmpString & delimt
            End If

            counter += 1
        End If
    Next

    If valueStrings <> String.Empty Then valueStrings = valueStrings.Substring(0, valueStrings.Length - delimt.Length)

    textSelection.Text = String.Format(output, hardStrings, valueStrings)
    DTE.UndoContext.Close()

End Sub

I felt the code was a little long to post here, but I posted an answer at my blog:
http://www.brianschmitt.com/2010/08/converting-concatenated-string-into.html

-- EDIT --
Per comment here is the relevant Macro - not sure why you cannot access...

Public Sub ConvertToStringFormat()
    DTE.UndoContext.Open("ConvertToStringFormat")
    Dim textSelection As TextSelection = DTE.ActiveDocument.Selection
    Dim output As String = "string.Format(""{0}"", {1})"
    Dim delimt As String = ", "
    Dim fmtdTostring As String = ".tostring("""

    Dim txtSelection As String() = System.Text.RegularExpressions.Regex.Split(textSelection.Text.Trim, "\+\s_[+\n\r\t]|&\s_[+\n\r\t]|\+|&")
    Dim hardStrings As String = String.Empty
    Dim valueStrings As String = String.Empty
    Dim counter As Int16 = 0

    For Each str As String In txtSelection
        Dim tmpString As String = str.Trim
        If tmpString.StartsWith("""") Then
            hardStrings &= tmpString.Substring(1, tmpString.Length - 2)
        Else
            Dim fmt As String = String.Empty
            Dim indxToString As Int32 = 0

            If tmpString.ToLower.Contains(fmtdTostring) Then
                indxToString = tmpString.ToLower.IndexOf(fmtdTostring)
                fmt = tmpString.Substring(indxToString + 11, tmpString.Length - tmpString.ToLower.IndexOf(""")", indxToString) - 1)
            End If

            If fmt <> String.Empty Then
                hardStrings &= "{" & counter.ToString & ":" & fmt & "}"
                valueStrings &= tmpString.Substring(0, indxToString) & delimt
            Else
                hardStrings &= "{" & counter.ToString & "}"
                valueStrings &= tmpString & delimt
            End If

            counter += 1
        End If
    Next

    If valueStrings <> String.Empty Then valueStrings = valueStrings.Substring(0, valueStrings.Length - delimt.Length)

    textSelection.Text = String.Format(output, hardStrings, valueStrings)
    DTE.UndoContext.Close()

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文