截断 2 个字符串并组合成更小的字符串

Truncating 2 Strings and combined into smaller String

所以,我有两个最大长度为 100 的字符串。

 Dim a as String ''has a max length of 100
 Dim b as String ''has a max length of 100

这两个字符串需要截断并组合成一个新字符串。

 Dim c as String 'has a max length of 100

我需要能够适当地截断每个字符串,以便我可以获得接近 100 的字符串 c。我打算做一堆 25 的语句来截断每个字符串。

 if a.length = 100 and b.length =0 then
     return a
 else if a.length = 100 andalso b.length <= 25 then
     return a.truncate(75) & b
 else if a.length = 100 andalso b.length <= 50 then
     return a.truncate(50) & b
 else if....

等一个打遍所有场景...

我觉得有更好的方法和更有效的方法,这样我就不会遇到像 a.length = 100 和 b.length = 51 这样的场景。我会截断更多然后需要字符。

有什么建议吗??请大家根据需要批评我。

编辑,这是 vb.Net..不是 C#(我在项目之间)抱歉!

我不想将它们加在一起并截断它们的原因是因为如果两个字符串的长度都是 100,它将完全截断第二个字符串。如果它们都是 100,那么我想将字符串 a 的长度截断为 50,将字符串 b 的长度截断为 50,因此当它们组合在一起时,它们总共为 100。换句话说,我需要两个字符串中的一些文本。

连接后截断它们,然后:

Dim c = a & b
If c.Length > 100 Then c = c.Remove(100)

如果您想尽可能多地保留每个字符串的开头:

Dim c = ""
If(a.Length > 50 AndAlso b.Length < 50)
  c = a.Remove(100 - b.Length) & b
Else If a.Length > 50 AndAlso b.Length > 50
  c= a.Remove(50) & b.Remove(50) 
Else 
  c = a & b
End if

If c.Length > 100 Then c = c.Remove(100)

虽然不完全您所要求的,但还有另一种选择...

Public Function WeirdConcatinate(a As String, b As String) As String
    Dim totalLen = a.Length + b.Length
    If totalLen > 100 Then
        Dim aLen = 100 * a.Length \ totalLen
        Dim bLen = 100 - aLen
        Return a.Remove(aLen) & b.Remove(bLen)
    Else
        Return a & b
    End If
End Function

这将为您提供每个字符串中的字符数(大约)与它们相互比较的长度成正比。如果两个字符串的长度相同,您将从每个字符串中得到 50。如果 a.Length = 100 且 b.Length = 50,则最终 a 为 66,b 为 34。

如果字符串的总长度大于限制,那么您可以根据它们的长度按比例取一小部分:

Module Module1

    Function CombineWithLengthConstraint(a As String, b As String, totalLength As Integer) As String

        ' trivial case 1:
        If totalLength < 1 Then
            Return String.Empty
        End If

        Dim aLen = Len(a)
        Dim bLen = Len(b)

        ' trivial case 2:
        If aLen + bLen <= totalLength Then
            Return a & b
        End If

        ' impossible-to-satisfy-equably case:
        If totalLength = 1 Then
            If aLen > 0 Then
                Return a.Substring(0, 1)
            ElseIf bLen > 0 Then
                Return b.Substring(0, 1)
            Else
                Return String.Empty
            End If
        End If

        ' aportion the lengths of the strings to be taken in the ratio of their lengths:
        Dim aFrac = CInt(Math.Round(aLen / (aLen + bLen) * totalLength, MidpointRounding.AwayFromZero))
        Dim bFrac = CInt(Math.Round(bLen / (aLen + bLen) * totalLength, MidpointRounding.AwayFromZero))

        ' ensure there is at least one character from each string...
        If aFrac = 0 Then
            aFrac = 1
            bFrac -= 1
        End If
        If bFrac = 0 Then
            bFrac = 1
            aFrac -= 1
        End If

        Dim aPart = a.Substring(0, aFrac)
        Dim bPart = b.Substring(0, bFrac)

        Return aPart & bPart

    End Function

    Sub Main()

        Dim a = New String("A"c, 10)
        Dim b = New String("b"c, 40)
        Dim c = CombineWithLengthConstraint(a, b, 10)

        Console.WriteLine(c)
        Console.WriteLine(Len(c))

        Console.ReadLine()

    End Sub

End Module

输出:

AAbbbbbbbb
10

如您所见,占字符总数 1/5 的第一个字符串最终贡献了结果的 1/5。

如果参数为 Nothing,VB.NET Len 函数返回 0。

我测试了它,将所有长度从 0 到 100 的两个字符串组合成一个长度为 100 的字符串,以防我在舍入或其他方面出错。

当然,如果在特定应用程序中有意义,您可以 return,比如说,字符串 b 的结尾部分而不是开始部分。

与其他一些答案一样,该算法可以解释。我的方法从每个字符串中获取,直到总共获取 100 个字符或字符串用完字符。

Private Function concat(a As String, b As String, length As Integer) As String
    Dim ca As New System.Text.StringBuilder()
    Dim cb As New System.Text.StringBuilder()
    For i As Integer = 0 To length - 1
        ca.Append(If(i >= a.Length, "", a(i)))
        cb.Append(If(i >= b.Length, "", b(i)))
        If ca.Length + cb.Length >= length Then Exit For
    Next
    Return (ca.ToString() & cb.ToString() & New String(" "c, 100)).Substring(0, length)
End Function

Sub Main()
    Dim a As String = New String("a"c, 0)
    Dim b As String = New String("b"c, 5)
    Dim c As String = concat(a, b, 100)
    Console.WriteLine($"'{c}'")
End Sub

'bbbbb '

(填充到 100 个字符,不在块引号中呈现)

Dim a As String = New String("a"c, 30)
Dim b As String = New String("b"c, 90)

'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'

Dim a As String = New String("a"c, 72)
Dim b As String = New String("b"c, 64)

'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'

(您在评论中的示例。72 >> 50、64 >> 50)