vb.net 增加字符串中的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19300788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:27:01  来源:igfitidea点击:

Increment character in a string

vb.net

提问by Jeff B

I have a 2 character string composed only of the 26 capital alphabet letters, 'A'through 'Z'.

我有一个仅由 26 个大写字母组成的 2 个字符串,'A'通过'Z'.

We have a way of knowing the "highest" used value (e..g "IJ"in {"AB", "AC", "DD", "IH", "IJ"}). We'd like to get the "next" value ("IK"if "IJ"is the "highest").

我们有办法知道“最高”使用值(例如"IJ"in {"AB", "AC", "DD", "IH", "IJ"})。我们想获得“下一个”值("IK"如果"IJ"是“最高”)。

Function GetNextValue(input As String) As String
  Dim first = input(0)
  Dim last = input(1)
  If last = "Z"c Then
    If first = "Z"c Then Return Nothing

    last = "A"c
    first++
  Else
    last++
  EndIf

  Return first & last
End Function

Obviously char++is not valid syntax in VB.NET. C# apparentlyallows you to do this. Is there something shorterless ugly than this that'd increment a letter? (Note: Option Strictis on)

显然char++在 VB.NET 中是无效的语法。C#显然允许你这样做。有没有比这更短的东西可以增加一个字母?(注:Option Strict开启)

CChar(CInt(char)+1).ToString

Edit: As noted in comment/answers, the above line won't even compile. You can't convert from Char -> Integer at all in VB.NET.

编辑:如评论/答案中所述,上面的行甚至无法编译。在 VB.NET 中根本无法从 Char -> Integer 转换。

回答by Jon Egerton

The tidiest so far is simply:

到目前为止最整洁的是:

Dim a As Char = "a"
a = Chr(Asc(a) + 1)

This still needs handling for the "z"boundary condition though, depending on what behaviour you require.

"z"不过,这仍然需要处理边界条件,具体取决于您需要的行为。

Interestingly, converting char++through developerfusion suggests that char += 1should work. It doesn't. (VB.Net doesn't appear to implicitly convert from char to int16 as C# does).

有趣的是,char++通过 developerfusion 进行转换表明char += 1应该可行。它没有。(VB.Net 似乎不像 C# 那样从 char 隐式转换为 int16)。

To make things really nice you can do the increment in an Extension by passing the char byref. This now includes some validation and also a reset back to a:

为了使事情变得非常好,您可以通过传递 char byref 在扩展中进行增量。这现在包括一些验证和重置回a

<Extension>
Public Sub Inc(ByRef c As Char)

    'Remember if input is uppercase for later
    Dim isUpper = Char.IsUpper(c)

    'Work in lower case for ease
    c = Char.ToLower(c)

    'Check input range
    If c < "a" Or c > "z" Then Throw New ArgumentOutOfRangeException

    'Do the increment
    c = Chr(Asc(c) + 1)

    'Check not left alphabet
    If c > "z" Then c = "a"

    'Check if input was upper case
    If isUpper Then c = Char.ToUpper(c)

End Sub

Then you just need to call:

然后你只需要调用:

Dim a As Char = "a"        
a.Inc() 'a is now = "b"

回答by rory.ap

Unfortunately, there's no easy way -- even CChar(CInt(char)+1).ToStringdoesn't work. It's even uglier:

不幸的是,没有简单的方法——甚至CChar(CInt(char)+1).ToString行不通。更丑的是:

CChar(Char.ConvertFromUtf32(Char.ConvertToUtf32(myCharacter, 0) + 1))

but of course you could always put that in a function with a short name or, like Jon E. pointed out, an extension method.

但当然,你总是可以把它放在一个带有短名称的函数中,或者像 Jon E. 指出的那样,一个扩展方法。

回答by UnhandledExcepSean

My answer will support up to 10 characters, but can easily support more.

我的回答最多支持 10 个字符,但可以轻松支持更多字符。

Private Sub Test
    MsgBox(ConvertBase10ToBase26(ConvertBase26ToBase10("AA") + 1))
End Sub

Public Function ConvertBase10ToBase26(ToConvert As Integer) As String
    Dim pos As Integer = 0

    ConvertBase10ToBase26 = ""
    For pos = 10 To 0 Step -1
        If ToConvert >= (26 ^ pos) Then
            ConvertBase10ToBase26 += Chr((ToConvert \ (26 ^ pos)) + 64)
            ToConvert -= (26 ^ pos)
        End If
    Next
End Function

Public Function ConvertBase26ToBase10(ToConvert As String) As Integer
    Dim pos As Integer = 0

    ConvertBase26ToBase10 = 0
    For pos = 0 To ToConvert.Length - 1
        ConvertBase26ToBase10 += (Asc(ToConvert.Substring(pos, 1)) - 64) * (26 ^ pos)
    Next
End Function

回答by dbasnett

Try this

尝试这个

Private Function IncBy1(input As String) As String
    Static ltrs As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim first As Integer = ltrs.IndexOf(input(0))
    Dim last As Integer = ltrs.IndexOf(input(1))
    last += 1
    If last = ltrs.Length Then
        last = 0
        first += 1
    End If
    If first = ltrs.Length Then Return Nothing
    Return ltrs(first) & ltrs(last)
End Function

This DOES assume that the code is only two chars, and are A-Z only.

这确实假设代码只有两个字符,并且只有 AZ。