2009-02-07 5 views
0

배경 :.Net- 분할 기능 또는 분할 방식을 사용해야합니까?

많은 단어의 문자열을 나중에 내 코드에서 나중에 사용할 수 있도록 나를 구분하는 배열로 분할해야했습니다. 그러나, 나는 내가 다음과 같이 내가 구분/구분 기호로 사용하고자 문자를 contaning 문자열을 선언 문자열에있을 수있는 번호를 제거하는 데 필요한 :

dim Separators As String = " 1234567890" 

그래서 내 코드가되었다 다소 다음과 같이

''//USING SPLIT-FUNCTION 
dim MyTestString as String = "This is 9 a 567 test" 
dim Separators As String = " 1234567890" 
dim ResultWithNoNumbers() as String 

ResultWithNoNumbers = Split(MyTestString,Separators.ToCharArray) 
Messagebox.Show(ResultWithNoNumbers.Length.ToString) 
''//Result is 1 and no split was performed 

이 그러나이 일

개별 문자로 내 구분을 고려하지 않은 분할 기능 때문에 작동하지 않았다 ..

''//USING SPLIT-METHOD 
dim MyTestString as String = "This is 9 a 567 test" 
dim Separators As String = " 1234567890" 
dim ResultWithNoNumbers() as String 

ResultWithNoNumbers = MyTestString.Split(Separators.ToCharArray, 
            _StringSplitOptions.RemoveEmptyEntries) 
Messagebox.Show(ResultWithNoNumbers.Length.ToString) 
''//Result is 4 and split was performed 

지금까지는 Split-function에 비해 Split-method에 더 많은 옵션이 있기 때문에 문제를 해결할 수있었습니다.

내 질문에, 단락 기호로 표준 공백 문자 ("") 만 필요하다고 가정 해 봅시다. 위의 예와 같이 숫자를 제거 할 필요가 없습니다. 그러면 두 가지 방법 모두 사용할 수 있습니다. 그럼 어느 쪽을 사용 하시겠습니까? 사용할 수있는 다양한 옵션을 제외하고 특정 옵션을 사용하면 어떤 이점이 있습니까? 아마도 하나는 더 빠르며 더 적은 메모리를 필요로합니까?

편집 : Windows Mobile 용으로 개발 중이므로 속도와 메모리 문제가 중요한 이유입니다.

감사합니다.

답변

2

내가 사용하는 것이 정규 표현식

Dim MyTestString As String = "This is 9 a 567 test 23424234 this is 
23 another test 23 and 3 again and one 34234 more" 
Dim reg_exp As New Text.RegularExpressions.Regex("\d") 
Dim reg_exp2 As New Text.RegularExpressions.Regex("\s{2,}") 

MyTestString = reg_exp.Replace(MyTestString, String.Empty) 
MyTestString = reg_exp2.Replace(MyTestString, " ") 

Dim strArr() As String 
strArr = MyTestString.ToString.Split(" ") 

Console.WriteLine(MyTestString) 
Console.ReadLine() 
,451,515,

출력 :

이것은이 다른 시험과 다시 하나 인 테스트입니다

+0

정규식은 확실히 여기 길을 가야하는 것입니다 더. – ctacke

+0

cstacke하려면 : 나는 당신이 WinMobile에 관해서 "전문가"라는 것을 알고 있습니다 ... 이것이 Regex (즉, Split-method 및 Split-function보다 속도 및 메모리 문제에 더 적합한)를 제안한 이유입니다. ? – moster67

+0

아니요, Regex becssue를 사용하면 최종 목표를 달성하는 데 더 간단하고 간단하게 처리 할 수 ​​있습니다. 나는 데스크톱에서도 똑같이 할 것이다. – ctacke

2

두 가지 모두에 대해 실행되는 코드는 다음과 같습니다. 스스로 결정하십시오. 나는 string.split()을 더 좋아한다.

: 당신이 분할() 함수를 호출 할 때 다음
Private Function InternalSplitKeepEmptyEntries(ByVal sepList As Integer(), ByVal lengthList As Integer(), ByVal numReplaces As Integer, ByVal count As Integer) As String() 
    Dim startIndex As Integer = 0 
    Dim index As Integer = 0 
    count -= 1 
    Dim num3 As Integer = IIf((numReplaces < count), numReplaces, count) 
    Dim strArray As String() = New String((num3 + 1) - 1) {} 
    Dim i As Integer = 0 
    Do While ((i < num3) AndAlso (startIndex < Me.Length)) 
     strArray(index++) = Me.Substring(startIndex, (sepList(i) - startIndex)) 
     startIndex = (sepList(i) + IIf((lengthList Is Nothing), 1, lengthList(i))) 
     i += 1 
    Loop 
    If ((startIndex < Me.Length) AndAlso (num3 >= 0)) Then 
     strArray(index) = Me.Substring(startIndex) 
     Return strArray 
    End If 
    If (index = num3) Then 
     strArray(index) = String.Empty 
    End If 
    Return strArray 
End Function 

가 실행됩니다 코드입니다 : 여기

사항 String.split()를 호출 할 때 실행됩니다 코드입니다
Private Shared Function SplitHelper(ByVal sSrc As String, ByVal sFind As String, ByVal cMaxSubStrings As Integer, ByVal [Compare] As Integer) As String() 
    Dim invariantCompareInfo As CompareInfo 
    Dim num2 As Integer 
    Dim ordinal As CompareOptions 
    Dim length As Integer 
    Dim num5 As Integer 
    Dim num6 As Integer 
    If (sFind Is Nothing) Then 
     length = 0 
    Else 
     length = sFind.Length 
    End If 
    If (sSrc Is Nothing) Then 
     num6 = 0 
    Else 
     num6 = sSrc.Length 
    End If 
    If (length = 0) Then 
     Return New String() { sSrc } 
    End If 
    If (num6 = 0) Then 
     Return New String() { sSrc } 
    End If 
    Dim num As Integer = 20 
    If (num > cMaxSubStrings) Then 
     num = cMaxSubStrings 
    End If 
    Dim strArray As String() = New String((num + 1) - 1) {} 
    If ([Compare] = 0) Then 
     ordinal = CompareOptions.Ordinal 
     invariantCompareInfo = Strings.m_InvariantCompareInfo 
    Else 
     invariantCompareInfo = Utils.GetCultureInfo.CompareInfo 
     ordinal = (CompareOptions.IgnoreWidth Or (CompareOptions.IgnoreKanaType Or CompareOptions.IgnoreCase)) 
    End If 
    Do While (num5 < num6) 
     Dim str As String 
     Dim num4 As Integer = invariantCompareInfo.IndexOf(sSrc, sFind, num5, (num6 - num5), ordinal) 
     If ((num4 = -1) OrElse ((num2 + 1) = cMaxSubStrings)) Then 
      str = sSrc.Substring(num5) 
      If (str Is Nothing) Then 
       str = "" 
      End If 
      strArray(num2) = str 
      Exit Do 
     End If 
     str = sSrc.Substring(num5, (num4 - num5)) 
     If (str Is Nothing) Then 
      str = "" 
     End If 
     strArray(num2) = str 
     num5 = (num4 + length) 
     num2 += 1 
     If (num2 > num) Then 
      num = (num + 20) 
      If (num > cMaxSubStrings) Then 
       num = (cMaxSubStrings + 1) 
      End If 
      strArray = DirectCast(Utils.CopyArray(DirectCast(strArray, Array), New String((num + 1) - 1) {}), String()) 
     End If 
     strArray(num2) = "" 
     If (num2 = cMaxSubStrings) Then 
      str = sSrc.Substring(num5) 
      If (str Is Nothing) Then 
       str = "" 
      End If 
      strArray(num2) = str 
      Exit Do 
     End If 
    Loop 
    If ((num2 + 1) = strArray.Length) Then 
     Return strArray 
    End If 
    Return DirectCast(Utils.CopyArray(DirectCast(strArray, Array), New String((num2 + 1) - 1) {}), String()) 
End Function