在字符串中的每个大写字母前放置一个空格

我想在字符串中的每个大写字母前加上空格。

所以转这个:TheQuickBrownFox

进入这个:The Quick Brown Fox

这是我到目前为止的代码:它在字符串中找到大写字符并在消息框中显示每个大写字母。
我不知道从这里去哪里:

Dim input As String = "TheQuickBrownFox"

For i As Integer = 0 To input.Length - 1
    Dim c As Char = input(i)

    If Char.IsUpper(c) Then
        MsgBox(c)
    End If
Next

我已经用谷歌搜索过,但找不到 Visual Basic 的解决方案。

zhangqiangwen88 回答:在字符串中的每个大写字母前放置一个空格

几个使用 LINQ 的例子:

Imports System.Linq
Imports System.Text

Dim input As String = "TheQuickBrownFox"
  • 如果您不知道,字符串是字符的集合,因此您可以使用 ForEach 循环(例如,For Each c As Char In input)迭代字符串内容。

▶ 从字符集合 (Enumerable(Of Char)) 中生成字符串,如果第一个大写字符是字符串中的第一个,则不包括第一个大写字符。
Select() 方法的第二个参数在指定时(在 Select(Function(c,i) ...)中)表示当前处理的元素的索引。
String.Concat()Enumerable(Of Char) 方法生成的 Select() 重建一个字符串:

Dim result = String.Concat(input.Select(Function(c,i) If(i > 0 AndAlso Char.IsUpper(c),ChrW(32) + c,c)))

相同,不考虑第一个大写字符的位置:

Dim result = String.Concat(input.Select(Function(c) If(Char.IsUpper(c),c)))

▶ 使用 aggregation function 作为累加器(仍然考虑第一个大写字符的位置)。

在处理字符串时,用作存储的 StringBuilder 可以使代码更高效(产生更少的垃圾)和更高的性能。
参见,例如,这里:StringBuilder

➨ 请注意,我正在向 StringBuilder 添加一个字符数组:

Dim result = input.Aggregate(New StringBuilder(),Function(sb,c) sb.Append(If(sb.Length > 0 AndAlso Char.IsUpper(c),{ChrW(32),c},{c})))

result 是一个 StringBuilder 对象:提取带有 result.ToString() 的字符串。

或者,和以前一样,不考虑位置:

Dim result = input.Aggregate(New StringBuilder(),c) sb.Append(If(Char.IsUpper(c),{c})))

▶ 上面的两个例子有点等价于一个简单的循环,它迭代字符串中的所有字符,然后创建一个新字符串或使用 StringBuilder 作为存储:

Dim sb As New StringBuilder()
For Each c As Char In input
    sb.Append(If(sb.Length > 0 AndAlso Char.IsUpper(c),{c}))
Next

如果您想在不考虑其位置的情况下向第一个大写字符添加一个空格,请按之前所述更改代码。

,

另一种选择是语言扩展方法。

Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        Demo()
        Console.ReadLine()
    End Sub

    Private Sub Demo()
        Dim data = New List(Of String) From
                {
                "ThisIsASentence","TheQuickBrownFox","ApplesOrangesGrapes"}

        data.ForEach(Sub(item) Console.WriteLine($"{item,-25}[{item.SplitCamelCase}]"))
    End Sub

End Module
Public Module StringExtensions
    <Runtime.CompilerServices.Extension>
    Public Function SplitCamelCase(sender As String) As String

        Return Regex.Replace(Regex.Replace(sender,"(\P{Ll})(\P{Ll}\p{Ll})","$1 $2"),"(\p{Ll})(\P{Ll})","$1 $2")
    End Function

End Module

enter image description here

,

您可以使用正则表达式:

Dim input = "TheQuickBrownFox"
Dim withSpaces = Regex.Replace(a,"(?<!^)([A-Z])"," $1")

正则表达式查找任何不在字符串开头之前的大写字母 A-Z 并将其捕获到一个组中。替换采用组内容并添加一个空格前缀

如果你不想使用否定的lookbehind,你可以修剪结果:

Dim withSpaces = Regex.Replace(a,"([A-Z])"," $1").TrimStart()

如果您不在乎字符串是否以空格开头,请不要修剪

,

要扩展您的代码:

Dim input As String = "TheQuickBrownFox"
Dim outputSB as new StringBuilder

For i As Integer = 0 To input.Length - 1
    Dim c As Char = input(i)

    If Char.IsUpper(c) Then
        outputSB.Append(" ")
        'MsgBox(c)
    End If

    outputSB.Append(c)
Next

Console.Writeline(outputSB.ToString())
本文链接:https://www.f2er.com/1316.html

大家都在问