Counting lines using CountFields(__,chr(13))

I need to count the number of lines in a large string. The value is stored as a global constant.

The following used to work:

lineCount = CountFields(AllQuestions,chr(13)) 

Now lineCount = 1, suggesting there were no chr(13)'s found. AllQuestions.len checks out, though, so I know the value is being accessed correctly.

Issue started after two things happened:

  • upgraded from Xojo2015_3.1 to Xojo2015_4.1. The project was originally written in RB2011, but compiled without a problem in Xojo2015_3.1.
  • changed one character typo in AllQuestions. I didn’t touch the line breaks.

As part of troubleshooting, I’ve tried cutting and pasting the whole text block from a plain text editor; maybe there is an encoding mismatch? I don’t think a TextInputStream would help because the string is stored in the program, not read from an external file.

End of Line isn’t always Chr(13). Use ReplaceLineEndings to normalize them to something you expect. Eg.,

linecount = CountFields(ReplaceLineEndings(AllQuestions, chr(13)), chr(13))

Bingo. What a handy function, who knew! Many thanks, @Tim Hare.