I'm trying to use strutils.removePrefix to remove the first 4 lines from a string, but i'm having trouble matching the first four lines. I'm guessing it has something to do with the newlines. How am i supposed to make this match? thanks.
#my code
varChunk = chunk
prefix = "\n" & stringChunkLineSeq[0] & "\n" & stringChunkLineSeq[1] & "\n"
echo "--------- start prefix -----------"
echo prefix
echo "--------- end prefix -------------"
echo varChunk.startsWith(prefix)
varChunk.removePrefix(prefix)
echo varChunk
#my terminal output.
--------- start prefix -----------
Content-Disposition: form-data; name="file1"; filename="price_list.txt"
Content-Type: text/plain
--------- end prefix -------------
false
Content-Disposition: form-data; name="file1"; filename="price_list.txt"
Content-Type: text/plain
$25 (5th line of varChunk)
While the text looks the same in both instances, it could have non-printable characters or terminal command chars that you can't normally see. On Linux you can use cli tools like od to inspect output in detail, e.g.:
$ nim r program.nim | od -a
0000000 nl C o n t e n t - D i s p o s i
0000020 t i o n : sp f o r m - d a t a ;
0000040 sp n a m e = " f i l e 1 " ; sp f
0000060 i l e n a m e = " p r i c e _ l
0000100 i s t . t x t " nl C o n t e n t
0000120 - T y p e : sp t e x t / p l a i
0000140 n nl nl $ 2 5 sp ( 5 t h sp l i n e
0000160 sp o f sp v a r C h u n k )
Or, if you're absolutely sure input data uses 'n' for line separation, you can just count newlines and remove enough chars from beginning using strutils.delete:
import std/strutils
proc removePrefixLines(s: var string, n: Positive) =
var ind, lines = 0
while lines < n:
if s[ind] == '\n': inc lines
inc ind
s.delete(0..ind-1)
varChunk.removePrefixLines(4)
echo varChunk
Thanks a lot to both of you!
I figured my \n was probably the reason for the non-matching condition, but was not sure what \n was supposed to be. I had searched before, but didn't find the info i needed. Spurred by your confirmations, i searched again, and my code now matches the input.
It was supposed to be:
prefix = "\r\n" & stringChunkLineSeq[0] & "\r\n" & stringChunkLineSeq[1] & "\r\n"