Replace a string by another

ombackeombacke Posts: 44Member
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 Posts: 8,123Member
    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 Posts: 107Member
    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 Posts: 386Member
    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 Posts: 8,123Member
    You must have tons of goodies in those archives Nelson :)
  • nelsonnelson Posts: 107Member
    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 Posts: 3Member
    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 Posts: 107Member
    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 Posts: 3Member
    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 Posts: 107Member
    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 Posts: 1Member
    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 Posts: 1,679Member
    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 Posts: 76Member
    I think more people than that guy are reading this thread ;), so it kinda still useful.
  • RockWithNAVRockWithNAV Posts: 836Member
    Is that solved what you are trying to achieve?
    If yes then please verify!!
  • TallyHoTallyHo Posts: 235Member
    Last active 2006-12-05.. I'd check Canadian gravestones ;)
Sign In or Register to comment.