web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Franz Kalchmair Blog / ReplaceString and a new Sel...

ReplaceString and a new SelectString

keoma Profile Picture keoma 32,729

With function ConvertStr you can replace substrings in strings. The original substring and the replacement have to have the same length. So what to do, if the length is different? Here is a solution.

ReplaceString(String : Text[250];OrigSubStr : Text[100];ReplSubStr : Text[100]) : Text[250]
// StartPos : Integer
StartPos := STRPOS(String,OrigSubStr);
WHILE StartPos > 0 DO BEGIN
String := DELSTR(String,StartPos) + ReplSubStr + COPYSTR(String,StartPos + STRLEN(OrigSubStr));
StartPos := STRPOS(String,OrigSubStr);
END;
EXIT(String);

SelectStr is a nice function to get text values out of a text line, simply giving the number/position of the value within the line, e.g. from ‘this,was,a,cool,thing’. here ‘was’ has position 2, so selectstr(2,line) would give us value ‘was’. SelectStr expects a comma as delimiter. Not so good, if you also have comma letters in the values (e.g. decimal values in europe). Would be fine to have a kind of SelectString function, where you can define the delimiter. hm … let’s try something.

SelectString(Number : Integer;String : Text[250];Delimiter : Char) : Text[100]
// i : Integer
// EndPos : Integer
// SubString : Text[100]
i := 1; // i : Integer
WHILE i <= Number DO BEGIN
i += 1;
EndPos := STRPOS(String,FORMAT(Delimiter));
IF EndPos > 0 THEN BEGIN
SubString := COPYSTR(String,1,EndPos-1);
String := DELSTR(String,1,EndPos);
END ELSE
SubString := String;
END;
EXIT(SubString);

To test that new function we use that:

// line : Text[250]
// i : Integer
line := ‘ab;cd;ef;cd’;
for i := 1 to 4 do
MESSAGE(SelectString(i,line,’;’));

cheers

 


Filed under: nav 2009, nav 2013, nav 2015, nav functions Tagged: replacestring, selectstring

Comments

*This post is locked for comments