Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

stringSplit & stringReplace

Uploader:RedakteurMOD
Datum/Zeit:27.09.2013 23:13:45

Sub stringSplit(text As String, separator As String, array() As String)
    Dim As Integer i, arrayIndex, lb = LBound(array)

    If @array(lb) = 0 Then
        arrayIndex = 0
        ReDim array(1)
        For i = 0 To Len(text) - 1
            If separator[1] = 0 Then
                If text[i] = separator[0] Then
                    arrayIndex += 1
                    ReDim Preserve array(arrayIndex)
                Else
                    array(arrayIndex) &= Chr(text[i])
                EndIf
            Else
                If Mid(text, i + 1, Len(separator)) = separator Then
                    arrayIndex += 1
                    i += Len(separator) - 1
                    ReDim Preserve array(arrayIndex)
                Else
                    array(arrayIndex) &= Chr(text[i])
                EndIf
            EndIf
        Next
    Else
        Dim As Integer ub = UBound(array)
        arrayIndex = lb
        For i = 0 To Len(text) - 1
            If separator[1] = 0 Then
                If text[i] = separator[0] Then
                    If arrayIndex < ub Then
                        arrayIndex += 1
                    Else
                        array(arrayIndex) &= Chr(separator[0])
                    EndIf
                Else
                    array(arrayIndex) &= Chr(text[i])
                EndIf
            Else
                If Mid(text, i + 1, Len(separator)) = separator Then
                    If arrayIndex < ub Then
                        arrayIndex += 1
                        i += Len(separator) - 1
                    Else
                        array(arrayIndex) &= Chr(text[i])
                    EndIf
                Else
                    array(arrayIndex) &= Chr(text[i])
                EndIf
            EndIf
        Next
    EndIf
End Sub

Function stringReplace( text As String, old_ As String, new_ As String ) As String
    Dim As String ret
    Dim As String array( )

    stringSplit( text, old_, array( ) )
    Dim As Integer i
    For i = LBound( array ) To UBound( array ) - 1
        ret &= array( i ) & new_
    Next
    ret &= array( i )
    Return ret
End Function

Print stringReplace( "abcabcabc", "ab", "d" )

Dim As String datum = "#Tag# #Monat# #Jahr#"
datum = stringReplace( datum, "#Tag#", "01" )
datum = stringReplace( datum, "#Monat#", "10" )
Print stringReplace( datum, "#Jahr#", "2013" )

Print stringReplace( "cabcc", "ab", "d" )
Print stringReplace( "cab", "ab", "d" )
Print stringReplace( "abc", "ab", "d" )
Sleep