Replace last space with character

sharon95sharon95 Member Posts: 183
Hi all, I would like to replace the last space of a string and put a new character. eg: from "Test something 1" to "Test something, 1"... I tried with some convertstr, delchr functions but nothing worked.. any idea? Thank you...

Answers

  • sharon95sharon95 Member Posts: 183
    ok, I'm done with that, the last doubt is how can I replace a part of a string?
    e.g.
    String -> hello world
    search -> wo
    replace with -> WO
    result -> hello WOrld
  • lubostlubost Member Posts: 623
    Generally no way. CONVERSTR converts all occurence of fromchars to tochars (https://docs.microsoft.com/en-us/dynamics-nav/convertstr-function--code--text-)
  • KishormKishorm Member Posts: 921
    You can do this using a dotnet string. Create a new variable of type System.String from mscorlib...

    'mscorlib, Version=4.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089'.System.String

    ...then use the Replace method...

    MyDotNetString := ‘hello world’;
    MyDotNetString := MyDotNetString.Replace(‘wo’,’WO’);
  • mucamuca Member Posts: 42
    edited 2018-07-13
    there is no function such, in this case I think You need to make Your own function
    can be like this

    LastSpacePos(Tstr : Text[30]) SpacePos : Integer
    SpacePos:=STRPOS(Tstr,' ');
    IF SpacePos = 0 THEN EXIT(0);
    Tstr:=COPYSTR(Tstr,SpacePos+1,30);
    WHILE STRPOS(Tstr,' ')>0 DO BEGIN
    SpacePos:=SpacePos+STRPOS(Tstr,' ');
    Tstr:=COPYSTR(Tstr,STRPOS(Tstr,' ')+1,30);
    END;

    so if there are no spaces in Tstr then return 0 like STRPOS (built-in function)

    You can add also a second parameter that will find any character and return last position in string ;)
Sign In or Register to comment.