Initalizing a string to certain characters

aztecconsultingaztecconsulting Member Posts: 55
What is the C/SIDE equalivent for the VB6 String() function?

This function takes 2 arguments:
Function String(Number As Long, Character) As String
The return value is a string of characters specified by the Character argument, repeated Number times.
String(4, "O") returns "OOOO".
String(4, 65) returns "AAAA".

Comments

  • DenSterDenSter Member Posts: 8,307
    Nothing like that in C/AL, but you could easily write one.
  • aztecconsultingaztecconsulting Member Posts: 55
    Actually I can't which is why I am posting the request here. I'm still learning C/SIDE without any training and only limited documentation. I am hoping the new user group will improve the situation but for now I have to rely on the kindness of strangers when I get stuck with an issue I can resovle myself.
  • davesdaves Member Posts: 49
    You could use the PADSTR function. You must have predefined your text variable to whatever maximum length you want to allow. Example
    xtest text 30

    The command
    xtest := PADSTR('',5,'Q');
    will give you a string of 'QQQQQ'

    The command
    xtest := padstr(xtest,maxstrlen(xtest),'m');
    will pad out the existing contents of xtest with m's. If it were applied right after the previous command, the result would be
    'QQQQQmmmmmmmmmmmmmmmmmmmmmmmmm'

    If that were followed by the command
    xtest := PADSTR('',7,'P');
    the result would be 'PPPPPPP'
    Dave Studebaker
    Co-Founder Liberty Grove Software
    Author: Programming Microsoft Dynamics NAV (V5.0), 2009, 2013
  • aztecconsultingaztecconsulting Member Posts: 55
    Thanks! That resolved it and is what I was looking for! I really wish some VB developer would write a guide to C/SIDE for Visual Basic Developers!
  • DenSterDenSter Member Posts: 8,307
    Well you could start a new codeunit for 'useful functions', let's give it number 50000. Inside that coduenit you open the globals (View/Globals) on the functions tab. Enter a new line and call it RepeatString. Then you click on the Locals button and you enter two parameters. One Integer called MyNumber and one text variable called MyString (set it to 1024 length). On the return value, you set the type to text, and call it ReturnString.

    Then in the trigger, you enter a local integer variable called Index, and you write the following code:
    ReturnString := '';
    FOR Index := 1 TO MyNumber DO BEGIN
      ReturnString := ReturnString + MyString;
    END;
    
    Of course you should also program some safeguards in it to check for maximum length and stuff like that, to prevent overruns.

    Now you can call this function from anywhere in Navision. All you need to do is declare the codeunit in your variables and call the RepeatString function with the values you specify.
  • DenSterDenSter Member Posts: 8,307
    Or you could use the PADSTR function :oops: forgot all about that one.
  • aztecconsultingaztecconsulting Member Posts: 55
    Yeah, I'm having to use padstr for some of the formatting already. One issue that just came us is that padstr pads left-justifed, what if I need right-justified? I mean I have a field with a value of 1000 but it's 6 bytes and the vendor expects 001000 using pad would return 100000 so that's not going to work.
  • nelsonnelson Member Posts: 107
    You can use this function:
    PadStrLeft(String : Text[250];Length : Integer;FillCharacter : Text[1]) NewString : Text[250]
    WHILE STRLEN(String) < Length DO
      String := FillCharacter + NewString;
    NewString := String;
    
    Hey, Daniel, it's from the same archives as the one from yesterday. :D
    Nelson Alberto
  • nelsonnelson Member Posts: 107
    I am hoping the new user group will improve the situation
    What new user group?!?
    Nelson Alberto
  • phil_sheuphil_sheu Member Posts: 3
    Hi,
    this is my version of PADSTR left.
    I found this thread after I finished my code.

    My code doesn't have no WHILE statement.
    And it also truncate the string if ln_len is smaller then lc_str (same as PADSTR)

    :D

    Phil :P

    >>>>>>>>>>>>

    PADL(lc_str : Text[255];ln_Len : Integer;lc_padChar : Text[1]) : Text[255]
    IF ln_Len > 255 THEN
    ERROR('Max length is 255');

    lc_str2 := COPYSTR(lc_str,1,ln_Len);
    lc_str1 := PADSTR('',ln_Len-STRLEN(lc_str2),lc_padChar);

    EXIT(lc_str1 + lc_str2);
Sign In or Register to comment.