Guest User

monkey-c-newline-cr.mc

a guest
May 1st, 2021
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // replace special chars ("\r", "\n") with space
  2.     function replaceSpecialChars(str) {
  3.         var charArray = str.toCharArray();
  4.         var filteredCharArray = [];
  5.         for (var i = 0; i < charArray.size(); i++) {
  6.             var filteredChar = charArray[i];
  7.             if (charArray[i] == 0x0d) { // or '\r' or '\u000d'
  8.                 filteredChar = ' ';
  9.                 // collapse "\r\n" into a single space
  10.                 if (i+1 < charArray.size() && charArray[i+1] == 0x0a) {
  11.                     i++;
  12.                 }
  13.             } else if (charArray[i] == 0x0a) { // or '\n' or '\u000a'
  14.                 filteredChar = ' ';
  15.             }
  16.             filteredCharArray.add(filteredChar);
  17.         }
  18.         return StringUtil.charArrayToString(filteredCharArray);
  19.  
  20.     }
  21.  
  22.     // Set the label of the data field here.
  23.     function testCase() {
  24.         var testString = "The\r\nquick\nbrown\rfox";
  25.  
  26.         System.println(testString.find("\r\n")); // prints: 3
  27.  
  28.         System.println(testString);
  29.         // prints:
  30. /*
  31. The\r
  32. quick
  33. brown\rfox
  34. */     
  35.         // Connect IQ translates \r to literal "\r" in console, and
  36.         // prints "\r" as box char on watch
  37.        
  38.         System.println(replaceSpecialChars(testString));
  39.         // prints:
  40. /*
  41. The quick brown fox
  42. */
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment