【问题标题】:Remove blank line in a string删除字符串中的空行
【发布时间】:2021-06-28 18:37:55
【问题描述】:

我想删除字符串中的空行如下:

"第一节

第二部分

第三节

我在每张内容幻灯片上显示一个滚动索引,因此当您单击幻灯片时,索引会突出显示您所在的部分。我不想显示子部分,所以我尝试替换以“-”开头的部分名称用“”,但这意味着我有空行。所以现在我想删除空行。

我试过了:

  • IIF 语句但用“”替换不会删除空行
  • 正则表达式,另一个链接建议使用以下模式:@"^\s+$[\r\n]*" 但 @ 会引发错误并且在任何情况下都不起作用

我尝试了类似以下的方法:

Dim RE As Object
Set RE = CreateObject("VBScript.RegExp")

With RE
    .Multiline = True
    .Global = True  
    resultString = .Replace(subjectString, "\s\n", string.empty)
    MsgBox resultString
End With

我在 stackoverflow 上找到的另一个潜在解决方案。

Dim xArr() as string
xArr = Split(TextBox1.Value, vbCrLf)
TextBox1.Value = ""

for i = 0 to Ubound(xArr)
    If Trim(xArr(i)) <> "" Then
        TextBox1.value = TextBox1.value & xArr(i) & vbCrLf
    End If
Next

【问题讨论】:

  • 我认为this 可能会帮助您解决问题。

标签: vba powerpoint


【解决方案1】:

看起来您的 RegEx 代码实际上是用于 VB.Net 而不是 VBA,下面的代码将 VBA 中的 n 个空白行替换为 1。

Dim RE As Object: Set RE = CreateObject("VBScript.RegExp")

With RE
    .MultiLine = True
    .Global = True
    .Pattern = "(\r\n)+"

    resultString = .Replace(subjectString, vbCrLf)

    MsgBox resultString
End With

当然,如果您只有 2 个空白行,您可以简单地:

resultString = replace$(subjectString, vbcrlf & vbcrlf, vbcrlf)

【讨论】:

  • 太棒了!谢谢 Alex K - 非常感谢您的帮助
【解决方案2】:

我知道这很旧,但这是我为提供帮助而制作的正则表达式公共函数。可能有更好的方法,但这对我来说很简单并且有效。

'=================================================================================='
Public Function RegExReplace(TextContent As String, SearchEx As String, Optional ReplaceEx As String = "", Optional _
                    EmptyLines As Boolean = False, Optional TrimLines As Boolean = True) As String
    Dim regEx As Object, strOutput As String
    Set regEx = CreateObject("vbscript.regexp")
    With regEx: .Global = True: .IgnoreCase = False: .MultiLine = True: .Pattern = SearchEx: End With
    TextContent = regEx.Replace(TextContent, ReplaceEx)
    If EmptyLines = False Then TextContent = RegExReplace(TextContent, "\r\n\r\n", "", True, False)
    If TrimLines = True Then TextContent = Trim(TextContent)
    
    RegExReplace = TextContent: Set regEx = Nothing
End Function
'=================================================================================='

【讨论】:

    【解决方案3】:

    如果出于某种原因您希望避免使用 RegEx(例如,在没有 VBScript 的 Mac 上工作),这里有一个纯粹的 VB 方法:

    Sub Test()
        Call TakeOutTheEmpties(ActiveWindow.Selection.ShapeRange(1))
    End Sub
    Sub TakeOutTheEmpties(oSh As Shape)
    
        Dim oPara As TextRange
        Dim x As Long
        
        If oSh.HasTextFrame Then
            If oSh.TextFrame.HasText Then
                For x = oSh.TextFrame.TextRange.Paragraphs.Count To 1 Step -1
                    Set oPara = oSh.TextFrame.TextRange.Paragraphs(x)
                    If oPara.Text = vbCr Then
                        oPara.Delete
                    End If
                Next
            End If
        End If
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 2011-03-24
      • 1970-01-01
      相关资源
      最近更新 更多