2017-05-29 43 views
-1

特定の文字列の右側を私に与える機能を開発中です。関数は分割されたcharに基づいて戻り文字列を取る可能性があります。文字列にcharを保持します。どちらかと言えば、戻り文字列はその文字区切り文字の最後の後になるか、取る文字列。誰かがそれを見て、それが正しいかどうか、またはそのコードに問題があるかどうかを確認できますか?特定の文字列で特定の文字列部分をとります

lastindexとして使用することを忘れないでください。私はlastindexofをtrueに設定し、false splitterCharPositionを設定する必要がある場合はsplitterCharPositionが何であってもかまいません。

はこれが私の現在のコードが確認されるようにされています

Public Function GetRightSideStringByCHar(splitterChar As String, searchingWord As String, keepCharAsWell As Boolean, lastindexof As Boolean, splitterCharPosition As Integer) As String 
     Dim index As Integer 
     Select Case lastindexof 
      Case False 
       index = GetNthIndex(searchingWord, splitterChar, splitterCharPosition) 
      Case True 
       index = searchingWord.LastIndexOf(splitterChar) 
     End Select 

     If index > 0 Then 
      If keepCharAsWell Then 
       searchingWord = searchingWord.Substring(0, index + splitterChar.Length) 
      Else 
       searchingWord = searchingWord.Substring(0, index) 
      End If 
     Else 
      searchingWord = String.Empty 
     End If 
     Return searchingWord 
    End Function 

ヘルパー関数は、n個のインデックスを取得する:あなたの他の削除された質問に

Public Function GetNthIndex(searchingWord As String, charseparator As Char, n As Integer) As Integer 
     Dim count As Integer = 0 
     For i As Integer = 0 To searchingWord.Length - 1 
      If searchingWord(i) = charseparator Then 
       count += 1 
       If count = n Then 
        Return i 
       End If 
      End If 
     Next 
     Return -1 
    End Function 
+0

ここで問題は何ですか?このコードを実行しましたか?それは動作しますか?あなたがレビューを求めているなら、https://codereview.stackexchange.com/でこの質問をすることができます。 –

答えて

0

をベースに、私はあなたのためにこれを策定しました。

以前のすべての例で動作しました。

[parameters] 
str = input string 
strtofind = separator string 
occurance = zero-based index to start cutting (optional) 
keepstringtofind = append separator to string (optional) 
righttoleft = operational direction (optional) 

    Public Function CutFromString(str As String, strtofind As String, Optional occurance As Integer = 0, Optional keepstrtofind As Boolean = False, Optional righttoleft As Boolean = False) As String 
    Dim s As String = str 
    Dim sections As List(Of String) = IIf(String.IsNullOrEmpty(str), New List(Of String), str.Split(strtofind.ToCharArray).ToList) 
    If sections.Count = 1 AndAlso sections.First = str Then sections.Clear() 
    If occurance < sections.Count Then 
     Dim b = sections.Count - occurance 
     While b > 0 
      If righttoleft Then 
       sections.RemoveAt(0) 
      Else 
       sections.RemoveAt(sections.Count - 1) 
      End If 
      b -= 1 
     End While 
     s = Join(sections.ToArray, strtofind) 
     If keepstrtofind And occurance > 0 Then 
      If righttoleft Then 
       s = strtofind + s 
      Else 
       s += strtofind 
      End If 
     End If 
    End If 
    Return s 
End Function 
+0

はすべてのケースでうまくいかないようです。この例では:Console.WriteLine(CutFromString( "1 34.1t0#$ 1.trweL24w222"、 "。"、1、True、True))はまだ始まっていません。 "、"。 "、1、True、True)は14.1001です。私は得る:2 – DinoDinn

+0

いくつかのバグを修正し、このバージョンを試してください – Mike