0

How to use code to remove extra information?

I have set up two text elements in the database, and text1 contains the following information.

2222
333


44553
1234


How to set up a button to remove the extra spaces in the content of text1? Only retain the following information in text2.

2222
333
44553
1234

(The spaces in each record are random.)

9 replies

null
    • Fred
    • 4 mths ago
    • Reported - view

    You can try:

    let xBreak := urlDecode("%0A");
    "// Split the text into lines";
    let lines := splitx('Text 2', "\n");
    "// Filter out empty lines";
    let nonEmptyLines := lines[!= ""];
    "// Join the non-empty lines back together with newline characters";
    let result := join(nonEmptyLines, xBreak);
    result
    

    I tried out ChatGPT for Ninox and while it didn't give me the correct answer it did lead me down this path.

    I go from:

    2222
    333
    
    
    444
    555
    
    

    to:

    2222
    333
    444
    555
    

    It suggested this:

    function removeEmptyLines(text : text) do
    // Split the text into lines
    let lines := split(text, "\n");
    // Filter out empty lines
    let nonEmptyLines := lines[!isempty(item(lines, i))];
    // Join the non-empty lines back together with newline characters
    let result := join(nonEmptyLines, "\n");
    result
    end;
    

    which didn't work. Line 3 should be splitx() not split(). Line 5, the editor didn't like it. Line 7 inserts the text "\n" and not the actual new line.

    • gold_cat
    • 4 mths ago
    • Reported - view
     said:
    I tried outJacques TUR ChatGPT for Ninox

     I also tried it! Haha, thank you for Fred's help.
     

    • John_Halls
    • 4 mths ago
    • Reported - view
    let cr := "
    ";
    join(split('Text 2', cr)[!= ""], cr)
    
      • gold_cat
      • 4 mths ago
      • Reported - view

       Another great perspective, thank you.

    • red_kite
    • 4 mths ago
    • Reported - view
    replacex('Text 2', "\n+", "
    ")
    
      • gold_cat
      • 4 mths ago
      • Reported - view

       I tried this code, but it doesn't remove leading spaces if the field starts with spaces.

      • red_kite
      • 4 mths ago
      • Reported - view

       You're right, improved here, but not so nice

      replacex(replacex(myText, "^\n+", ""), "\n+", "
      ")
      
      • red_kite
      • 4 mths ago
      • Reported - view

       for leading and trailing spaces

      replacex(replacex(myText, "^\n+|\n+$", ""), "\n+", "
      ")
      
      • gold_cat
      • 4 mths ago
      • Reported - view

       Thank you, MZ, for the new method.

Content aside

  • Status Answered
  • 4 mths agoLast active
  • 9Replies
  • 75Views
  • 4 Following