none
How to detect Carriage Return (character) within a text string?

    Question

  • Hello. How would one check for the location of the CR within a text string in SmallBasic? Im using the Text.GetSubText method, but checking for character code 13 or "^M" does not seem to work.  Btw. I'm using Notepad to generate the TXT file, and when I read it in and print it out within my SB program, it would appear that the CR takes up two characters (?) Any ideas?
    Wednesday, January 26, 2011 9:22 PM

Answers

  • Good thinking, litdev. When I used your idea above on my sample.txt (generated in Notepad), at the end of every line I discovered three extra characters: 13 (CR), no char code (?), and 10 (Line Feed) -- more data than I expected, but.. it became obvious that I should use the GetCharacter method, thus:

        crIndex = Text.GetIndexOf(sample,Text.GetCharacter(13))

    This returns the index of the first CR in the text file, which is what I needed.

    Thanks!

    • Marked as answer by PeterDad Thursday, January 27, 2011 5:23 AM
    Thursday, January 27, 2011 5:22 AM

All replies

  • I don't think SmallBasic can handle text with a CR in it.  My approach would be to read the text into and array of text records with no CR in each. - With some more looking I think it can - see EDITs below.

    EDIT

    A little more playing and LineFeed (LF) seems to work on output to TextWindow and GraphicsWindow.

    TextWindow.WriteLine("LF character is ASCII 10"+Text.GetCharacter(10)+"and seems to work here")
    GraphicsWindow.DrawText(50,50,"LF character is ASCII 10"+Text.GetCharacter(10)+"and seems to work here")

    EDIT2

    Reading your earlier post on how to read through a text string I understand better the question, and have done some testing and understand a bit better how it is working (I think).

    If you have a text string that you have read in that contains some CR or LF characters, you could try writing out all the ASCII codes in the string to possibly identify the CR or LF combination, here the LF seems to have an extra unknown character before it - on second thoughts this is just the LF printing itself as a new line! - perhaps this is the double characters you are 'seeing' when you print out character by character?

    sample = "sample "+Text.GetCharacter(10)+"text"

    For i = 1 To Text.GetLength(sample)
      char = Text.GetSubText(sample,i,1)
      TextWindow.WriteLine(char +" : "+Text.GetCharacterCode(char))
    EndFor

    When I read a text file that contains multiple lines using File.ReadContents, it is the ASCII/UNICODE 10 that appears in the text string I read.

    Wednesday, January 26, 2011 9:55 PM
    Moderator
  • Good thinking, litdev. When I used your idea above on my sample.txt (generated in Notepad), at the end of every line I discovered three extra characters: 13 (CR), no char code (?), and 10 (Line Feed) -- more data than I expected, but.. it became obvious that I should use the GetCharacter method, thus:

        crIndex = Text.GetIndexOf(sample,Text.GetCharacter(13))

    This returns the index of the first CR in the text file, which is what I needed.

    Thanks!

    • Marked as answer by PeterDad Thursday, January 27, 2011 5:23 AM
    Thursday, January 27, 2011 5:22 AM