Android Question Replace characters between two characters in a text file.

wolfray1972

Member
Licensed User
Hello all,

I have a textfile with a line like this:

.1.|Hello world |00000

Now my question is how to replace "Hello world" to lets say "bye bye world"

Hope you could help me.
 

emexes

Expert
Licensed User
In the working(?) Log1.txt and Log2.txt there are 17 characters between the vertical bars, and in Log3.txt there is 18 characters between the vertical bars.

What happens if you change your program so that it outputs a Log3.txt file with 17 characters between the vertical bars?

I am mildly confused by the random extra single space between the NUL and CRLF on some lines in Log1.txt. Presumably this file is created by a program that would be unlikely to act so arbitrarily. Unless perhaps the spaces mark the end of a transaction, or a reboot of the program.

1604235213699.png
 
Last edited:
Upvote 0

wolfray1972

Member
Licensed User
Thnx Emexes for you help. I will try create new file. But Erel and i believe you said chr() is not good to use. I now use this code to write log.dat Is there a beter way? Or is this a good way. I can test tommorow if this works. Just want to know if i am using the correct way.

B4X:
Dim TextWriter1 As TextWriter
    TextWriter1.Initialize(File.OpenOutput(File.DirRootExternal, "Log.dat",True))
   
    Dim ListCount As Int
    Dim ListCount1 As Int
    Dim ArtValue As String
    Dim Aantal As String
    ListCount = 0
    ListCount1 = 0
   
    Aantal = ListView2.GetItem(ListCount1)
    ArtValue = ListView1.GetItem(ListCount)
   
    Do While ListCount  < ListView1.Size
        ArtValue = ListView1.GetItem(ListCount)
        Aantal = ListView2.GetItem(ListCount1)
        TextWriter1.WriteLine(".2.|" & WonumText.Text.Replace("WO:","%")& "|000000.00"  & Chr(0) &   Chr(10) & Chr(13) & ".2.|" & ArtValue & "|" & "00000" & Aantal & ".00" & Chr(0))
        ListCount = ListCount + 1
        ListCount1 = ListCount1 + 1
        Log(".2.|" & WonumText.Text.Replace("WO:","%")& "|00000.00"  & Chr(0) &  CRLF & ".2.|" & ArtValue & "|" & "0000" & Aantal & ".00" & Chr(0)  & Chr(10) & Chr(13))
    Loop
    TextWriter1.Close
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
It's a constant tab length file as far as I can see, so replace will not work properly if you don't insert spaces in case they are needed or cut ones in case the replacement exceeds the size of the tab.

Unfortunately, never got into regex patterns efficiently, so here's an alternative which you may want to try (not tested however):

B4X:
Sub replaceWithSpacesIfNeeded(searchIn As String,cr As String,delimiter As String,target As String,replacement As String) As String 
    
    '[searchIn]=original text
    '[cr]=character used as lines' separator
    '[delimiter]=character used as tabs' separator
    '[target]=string we want to replace by [replacement] string
    
    Private arr1() As String=Regex.Split(cr,searchIn)
    Private finalStringBuild As StringBuilder
    finalStringBuild.Initialize
    For i=0 To arr1.Length-1
        Private arr2() As String=Regex.Split(delimiter,arr1(i))
        For j=0 To arr2.Length-1
            Private startIndex As Int
            startIndex=arr2(j).IndexOf(target)
            If startIndex=0 Then 
                Private endIndex As Int=arr2(j).Length
                Private length As Int=endIndex-startIndex
                Private replacementLength As Int=replacement.Length
                Private finalReplacement As String=replacement
                If length>replacementLength Then 
                    For k=1 To length-replacementLength
                        finalReplacement=finalReplacement&" "
                    Next
                else if length<replacementLength Then 
                    finalReplacement=finalReplacement.SubString2(0,length)
                End If
                arr2(j)=finalReplacement
            End If
            finalStringBuild.Append(arr2(j))
            If j=arr2.Length-1 Then 
                finalStringBuild.Append(cr)
            Else 
                finalStringBuild.Append(delimiter)
            End If
        Next
    Next
    Private finalString As String=finalStringBuild.ToString
    Return finalString
    
End Sub
 
Upvote 0

emexes

Expert
Licensed User
I now use this code to write log.dat Is there a better way?

Try this, which simply constructs new lines from scratch, no need to do replacements:
B4X:
'replace this line:
'TextWriter1.WriteLine(".2.|" & WonumText.Text.Replace("WO:","%")& "|000000.00"  & Chr(0) &   Chr(10) & Chr(13) & ".2.|" & ArtValue & "|" & "00000" & Aantal & ".00" & Chr(0))

'with these four lines:
Dim LotsOfSpaces As String = "                    "    'at least 17 spaces

Dim TempDescription As String = (Aantal & LotsOfSpaces).SubString(0, 17)    'right-pad string to 17 characters long
Dim TempValue As String = NumberFormat2(ArtValue, 6, 2, 2, False)    'should format number as 000000.00

TextWriter1.WriteLine(".2.|" & TempDescription & "|" & TempValue & Chr(0))    'WriteLine should add on the CR LF
 
Last edited:
Upvote 0

advansis

Active Member
Licensed User
Longtime User
Using regex.split:
B4X:
Sub Button1_Click
    Dim SepChar As String="\|"
    Dim myString As String=".1.|Hello world |00000" & CRLF & ".2.|Hi guys |00001"
    Dim arrS As List=Regex.Split(CRLF,myString)
    LogColor(myString,Colors.Yellow)
    For i=0 To arrS.Size-1
        Dim arrL As List=Regex.Split(SepChar,arrS.Get(i))
        arrL.set(1,"bye bye world")
        arrS.Set(i,Join(arrL,SepChar))
    Next
    myString=Join(arrS,CRLF)
    LogColor(myString,Colors.Cyan)
End Sub

Public Sub Join(lst As List, joinChar As Object) As String
 
    Dim retStr As StringBuilder
    retStr.Initialize
 
    For Each str In lst
        retStr.Append(str).Append(joinChar)
    Next
 
    Return retStr.ToString
 
End Sub

I used this code snippet: JOIN Code Snippet

Hope this helps
 
Upvote 0
Top