Ignore:
Timestamp:
Jun 29, 2008, 11:42:27 AM (16 years ago)
Author:
OverTaker
Message:

String.Split,Joinメソッド実装

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ab5.0/ablib/src/Classes/System/String.ab

    r497 r531  
    547547        End Function
    548548
     549        Function Split(separator As System.Collections.Generic.List<String>) As System.Collections.Generic.List<String>
     550            Return Split(separator, -1, StringSplitOptions.None)
     551        End Function
     552
     553        Function Split(separator As System.Collections.Generic.List<String>, options As StringSplitOptions) As System.Collections.Generic.List<String>
     554            Return Split(separator, -1, options)
     555        End Function
     556
     557        Function Split(separator As System.Collections.Generic.List<String>, count As Long, options As StringSplitOptions) As System.Collections.Generic.List<String>
     558            Dim split As System.Collections.Generic.List<String>
     559            Dim index As Long, t1Index As Long, t2Index As Long
     560            Dim s As String, substring As String
     561            Dim flag As Boolean
     562
     563            Do
     564                t1Index = Length
     565                flag = True
     566                Foreach s In separator
     567                    t2Index = IndexOf(s, index)
     568                    If t2Index > -1 Then
     569                        t1Index = Math.Min(t1Index, t2Index)
     570                        flag = False
     571                    End If
     572                Next
     573
     574                substring = Substring(index, t1Index - index)
     575                If Not ( IsNullOrEmpty(substring) and (options = StringSplitOptions.RemoveEmptyEntries) ) Then
     576                    split.Add(substring)
     577                End If
     578
     579                If flag Then Return split
     580                If split.Count = count Then Return split
     581               
     582                index = t1Index + 1
     583            Loop
     584        End Function
     585
     586        Static Function Join(separator As String, strings As System.Collections.Generic.List<String>) As String
     587            Return Join(separator, strings, 0, strings.Count)
     588        End Function
     589
     590        Static Function Join(separator As String, strings As System.Collections.Generic.List<String>, startIndex As Long, count As Long) As String
     591            If (startIndex+count > strings.Count) or (startIndex < 0) or (count < 1) Then
     592                Throw New ArgumentOutOfRangeException("String.Join: One or more arguments are out of range value.", "startIndex or count")
     593            End If
     594
     595            Dim string As String
     596            Dim i As Long
     597            For i = startIndex To startIndex + count - 2
     598                string += strings[i] + separator
     599            Next
     600            Return string + strings[i]
     601        End Function
     602
    549603        Const Function ToLower() As String
    550604            Dim sb = New Text.StringBuilder(m_Length)
     
    669723    End Class
    670724
     725    Enum StringSplitOptions
     726        None
     727        RemoveEmptyEntries
     728    End Enum
     729
    671730End Namespace
Note: See TracChangeset for help on using the changeset viewer.