Replace a string by another

ombackeombacke Member Posts: 44
I want to change every occurance of & to and
The CONVERTSTR function does not let me do that since & and and have different number of characters..
Does anyone know of a way of doing this??

Comments

  • DenSterDenSter Member Posts: 8,307
    I would probably write a concatenation function. You get the position of the &, and then you cut whatever is in front of it, do "+ 'and' +" and then whatever is after it.
  • nelsonnelson Member Posts: 107
    Here, you can use this function as is.
    It's from the archives...:wink::
    ReplaceString(String : Text[250];FindWhat : Text[250];ReplaceWith : Text[250]) NewString : Text[250]
    WHILE STRPOS(String,FindWhat) > 0 DO
      String := DELSTR(String,STRPOS(String,FindWhat)) + ReplaceWith + COPYSTR(String,STRPOS(String,FindWhat) + STRLEN(FindWhat));
    NewString := String;
    

    [edit]
    Oh, and then you just use the function as:
    ReplaceString('String which contains &','&','AND');
    
    The return will be:
    'String which contains AND'
    
    Nelson Alberto
  • ShenpenShenpen Member Posts: 386
    Heh :) Some day or later, everbody needs to write a decent string manipulation codeunit in navision - reverse, replace, ltrim, rtrim, alltrim... :) It's all about for cycles and char variables.

    Do It Yourself is they key. Standard code might work - your code surely works.
  • DenSterDenSter Member Posts: 8,307
    You must have tons of goodies in those archives Nelson :)
  • nelsonnelson Member Posts: 107
    I have quite a few codeunits which can be imported to any database as they don't depend on any other object.
    Stuff like String Handling, Array Handling, Excel, Word and Outlook Automation, File Manipulation, Date and Time utilities, etc.

    Well, maybe you can't just import a few of them.
    Mostly the Microsoft Office stuff depends on Office being installed, of course. :roll: :D
    Nelson Alberto
  • piteruanspiteruans Member Posts: 3
    hi everybody!
    I'm a navision newbie so please don't shoot me!!! :P
    I searched for those "archives" in all mibuso.com, but I didn't find them, could you please give me a link or something similar?
    thanks a lot!

    P
  • nelsonnelson Member Posts: 107
    I actually meant my archives... it's stuff I collected over the years and which I have lying around in databases and FOB's.
    So, no link which I can give you. sorry.
    Nelson Alberto
  • piteruanspiteruans Member Posts: 3
    ah ok, I supposed it was like this...
    so, I disturb you a little bit again...could you post some string manipulation function or mail me a little fob with these functions?
    I'm looking for some "stupid" but very useful functions like trim, reverse and similar, but I haven't idea of how to create them...

    thanks for answering!

    P.
  • nelsonnelson Member Posts: 107
    Well, string manipulation functions are really not that hard.
    When you are in the C/AL editor, use the Symbol Menu (F5) to see all the existing string functions.
    You will find them in SYSTEM, String. With these you should be able to tackle most string-related problems.

    For example, in order to trim a string you can use the DELCHR function:
    DELCHR(String,'<>') will delete spaces at the beginning and end of the string.
    Nelson Alberto
  • KirillKirill Member Posts: 1
    Hi there!

    If you want to replace a substring with new subtring which contains the same charachters, the ReplaceString function by nelson will cycling over the loop. So you can use the following function:
    LOCAL ReplaceString(String : Text;FindWhat : Text;ReplaceWith : Text) NewString : Text
    FindPos := STRPOS(String,FindWhat);
    WHILE FindPos > 0 DO BEGIN
      NewString += DELSTR(String,FindPos) + ReplaceWith;
      String := COPYSTR(String,FindPos + STRLEN(FindWhat));
      FindPos := STRPOS(String,FindWhat);  
    END;
    NewString += String;
    

    For example, if you want escape characters in XML you can use:
    ReplaceString(XMLBody,'"','\"');
    

    Input:
    <FullName>Публичное акционерное общество "Рога и Копыта"</FullName>

    Output:
    <FullName>Публичное акционерное общество \"Рога и Копыта\"</FullName>
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2018-10-04
    @Kirill do you really think that the guy hasn't figured it out during those 12.5 years? :wink:
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Maria-SMaria-S Member Posts: 90
    I think more people than that guy are reading this thread ;), so it kinda still useful.
  • RockWithNAVRockWithNAV Member Posts: 1,139
    Is that solved what you are trying to achieve?
    If yes then please verify!!
  • TallyHoTallyHo Member Posts: 416
    Last active 2006-12-05.. I'd check Canadian gravestones ;)
Sign In or Register to comment.