去除字符串中的空行
Remove blank line in a string
我想删除字符串中的空行,如下所示:
"第一节
第二节
第三节
我在每张内容幻灯片上显示一个滚动索引,这样当您单击幻灯片时,索引会突出显示您所在的部分。我不想显示小节,所以我尝试替换以“开头的部分名称-" 和 "",但这意味着我有空行。所以现在我想删除空行。
我试过了:
- IIF 语句但替换为“”不会删除空行
- 正则表达式,另一个 link 建议使用以下模式:@"^\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
我在 Whosebug 上找到的另一个可能的解决方案。
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
看起来您的 RegEx 代码实际上是针对 VB.Net 而不是 VBA,下面的代码将 n 空行替换为 [=21] 中的 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)
我知道这已经过时了,但这里有一个正则表达式 Public 函数是我用来提供帮助的。可能有更好的方法,但这对我来说很简单并且有效。
'=================================================================================='
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
'=================================================================================='
如果出于某种原因您希望避免使用 RegEx(例如在 Mac 上工作,其中 VBScript 不可用),这里有一个纯粹的 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
我想删除字符串中的空行,如下所示:
"第一节
第二节
第三节
我在每张内容幻灯片上显示一个滚动索引,这样当您单击幻灯片时,索引会突出显示您所在的部分。我不想显示小节,所以我尝试替换以“开头的部分名称-" 和 "",但这意味着我有空行。所以现在我想删除空行。
我试过了:
- IIF 语句但替换为“”不会删除空行
- 正则表达式,另一个 link 建议使用以下模式:@"^\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
我在 Whosebug 上找到的另一个可能的解决方案。
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
看起来您的 RegEx 代码实际上是针对 VB.Net 而不是 VBA,下面的代码将 n 空行替换为 [=21] 中的 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)
我知道这已经过时了,但这里有一个正则表达式 Public 函数是我用来提供帮助的。可能有更好的方法,但这对我来说很简单并且有效。
'=================================================================================='
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
'=================================================================================='
如果出于某种原因您希望避免使用 RegEx(例如在 Mac 上工作,其中 VBScript 不可用),这里有一个纯粹的 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