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

Put a space in front of every capital letter in a string

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

所以转这个: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 的解决方案。

几个使用 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()Select() 方法生成的 Enumerable(Of Char) 重建一个字符串:

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), ChrW(32) + c, c)))

▶ 将 aggregation function that uses a 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(), 
    Function(sb, c) sb.Append(If(Char.IsUpper(c), {ChrW(32), 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), {ChrW(32), c}, {c}))
Next

如果要在第一个大写字符中添加 space 而不考虑其位置,请按前面所述更改代码。

您可以使用正则表达式:

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

正则表达式查找前面没有字符串开头的任何大写字母 A-Z,并将其捕获到一个组中。替换采用组内容和前缀 space

如果你不想使用负向回顾,你可以trim结果:

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

或者如果您不关心字符串是否以 space

开头,则不要 trim

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

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})", " "),
                             "(\p{Ll})(\P{Ll})",
                             " ")
    End Function

End Module

扩展您的代码:

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())