Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

find a new line in a string

(0) ShareShare
ReportReport
Posted on by 161

Hi

As i am new to BC programming I am unsure of the correct syntax to find a new line in Text string. Can anyone advise?

As a test i am using the code to find the first '.'  (this works perfect!)
textstring := 'This is line1. this is line 2. this is line 3';
crpos := strpos(longtextleftover, '.');
but i want to change this to a new line.
textstring := 'This is line1
this is line 2
this is line 3';
crpos := strpos(longtextleftover, ???????? );

Any idea's appreciated. If it's a simple answer I apoloogise for my lack of knowledge in advance.
Thanks Neil.
  • Suggested answer
    Neil Phelan Profile Picture
    Neil Phelan 161 on at
    RE: find a new line in a string

    Thanks all for the help.

    I solved it using the following in the end...

    var

    typehelp: Codeunit "Type Helper";

    crlf: Text[2];

    begin

           crlf := typehelp.CRLFSeparator();

    textstring := 'This is line1

    this is line 2

    this is line 3';

    //find the first line break

    crpos := strpos(longtextleftover, crlf[2]  );

    end

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 993 Super User 2024 Season 1 on at
    RE: find a new line in a string

    If it is in a string you import or get from another system you can also use the CHAR datatype to search for the value 10 or 13 that is the char values for linefeed and carriage return.

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 75,848 Super User 2024 Season 2 on at
    RE: find a new line in a string

    Hi, Lars' answer is very good, he splits the line based on the content of textstring.

    But I would like to add some basic information

    when you want to change a line in the text, just use '\'.

    For example:

    pastedimage1646874501625v2.png

    pastedimage1646874453219v1.png

    So use '\' will find a new line.

    A simple example:

    pastedimage1646874633017v4.png

    pastedimage1646874619736v3.png

    Hope this will help.

    Thanks.

    ZHU

  • RE: find a new line in a string

    Hello,

    You got 2 out of 3 commands that you need :-), just add COPYSTR([String],[Position],[Length]);. Something like this:

       begin

           textstring := 'This is line1. this is line 2. this is line 3';

           LinePos := StrPos(TextString, '.');

           line1 := CopyStr(TextString, 1, LinePos);

           TextString := CopyStr(TextString, LinePos + 1);

           LinePos := StrPos(TextString, '.');

           line2 := CopyStr(TextString, 1, LinePos);

           TextString := CopyStr(TextString, LinePos + 1);

           line3 := TextString;

           Message(Line1 + '\' + Line2 + '\' + Line3);

       end;

       var

           TextString, Line1, Line2, Line3 : Text;

           LinePos: Integer;

    Note, I have not tested this, and likely I got a position wrong a few places, so it likely needs to be adjusted. Add more MESSAGE to see how the string develops.

    Once that works, of course then the next challenge would be to do this as a function, something like this:

    WHILE STRPOS(TextString,'.') <> 0 DO

     TextString := MyFunction(TextString);

    hth

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,735 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans