find() cannot find escape characters

Hello,

My app processes a text input that the user inputs into the settings.  Because users will sometimes copy and paste their parameters into the settings from a webmail apps, the text may include some &NewLine (0x0d0a) characters.  They appear to be NewLine characters, because when I go to display them in the function drawText(), they show a new line in the middle of the text.  And I copied the text from my web email app to notepad+ hex editor and sure enough they appear as new lines (0x0d0a) character sequence.  It even prints out a new line when I print that same output to the debug output using System.println(), I see new lines on the log file where I suspect new lines are in the text!

I want to remove any and all of those characters from the setting string, but using String.Find() does not appear to work:

function RemoveNewLines(textInput) { 
		var newLine = (0x0D0A).toString(); 
		//var newLine = "/n"; // this does not work either
		var textOutput = textInput;
		
		System.print("RemoveNewLines: ");
		while (true) {
			var index = textOutput.find(newLine); 
			if (index != null) {
				System.print("found newline at " + index + ",");
				textOutput = textOutput.substring(0, index) + textOutput.substring(index + 2, textOutput.length());
			} else {
				System.println(" that's all.");
				return textOutput;
			}
		}
		return null; 
	}

This always seems to produce "RemoveNewLines:  that's all."  which indicates the find() function did not find any new lines in the textInput.

(This is slightly more difficult to test, since to do so you need to side load an app to your watch and use garmin connect app to update the settings.  The simulator settings editor does not accept newlines, but my Android app does)  I am certain that new lines from textual settings input is getting into the data, because when my app does drawText() using some of that text, on my watch it unusually appears with a new line smack dab in the middle of the text!

Is it possible to search for a new line using the Find() function?

I've also tried the following, but it doesn't seem to remove the newline.. maybe I'll try output = textInput.substring(2,textInput.length()); removing two chars instead of one...

	// target the newline which appears at the beginning of the textInput
	var output;
	if (textInput.substring(0,1).equals("\n")) { // THIS CONDITION IS FOUND TRUE
		System.println("Removed Newline before point data");
		output = textInput.substring(1,textInput.length()); // hmm
	}

  • The toString you're using in the find isn't the char 0x0d and then the character 0x0a.  Do a println on newLine and you'll see "3338"

    You want to build a string with the two characters 0x0d and 0x0a.  Maybe look at toChar().

    Also, are you sure the strings have the same line terminators you want to find?

  • As Jim pointed out (0x0D0A).toString() is "3338". It converts the number 0x0d0a to a string, not the two chars 0x0d and 0x0a.

    The closest (in spirit) version of that code which works would be as follows (although I don't recommend it bc there's better alternatives below, for your specific use case):

    StringUtil.charArrayToString([0x0d.toChar(), 0x0a.toChar()]); // A very verbose and inefficient way of typing "\r\n" or "\u000d\u000a"

    ---

    However, you should also be able to specify newlines and carriage returns within strings/chars as standard "printf-style" escape sequences or unicode escape sequences :

    Carriage return: "\r" or "\u000d"

    Newline: "\n" or "\u000a"

    I noticed that CIQ will always render "\r" as a square (unknown char) on the watch whether or not it precedes a newline (which is rendered properly in most cases). On the console (System.println), "\r" is rendered as the literal text "\r".

    I don't think calling string.find() over and over again is the most efficient implementation tho, although it will work.

    Try something like this (converting the string to a CharArray and comparing each individual char to '\r' / 0x0d / '\u000d' or '\n' / 0x0a / '\u000a').

    (I included a working version of the find() call you wanted, but I still wouldn't use it for this.)

    https://pastebin.com/Fv18tMEq

    Sorry, wish I could post the code properly, but the forum blocked it.

    You could easily mod the code to simply drop "\r" and "\n" instead of replacing them with a space, but that might cause words to run together in the input.