Formatting string

westerwester Member Posts: 33
edited 2009-11-09 in Navision Financials
Hi

If I have a string consisting of all uppercased letters like: "MY NAME", and I want to convert it to a more readable format like: "My name" or maybe: "My Name" before I insert it to the database.

Does anybody know how to do that i C/AL?

:wink:

Comments

  • DakkonDakkon Member Posts: 192
    No problem:)
    if varName is your variable
    newval := UPPERCASE(COPYSTR(varName,1,1)) + LOWERCASE(COPYSTR(varName,2));

    That will capitalize your first letter and lowercase the rest, simple enough, no?:)
    Thad Ryker
    I traded my sanity for a railgun :mrgreen:
  • DenSterDenSter Member Posts: 8,304
    Yes that's very simple, only it changes 'MY NAME' to 'My name', and he wants the first letter of the second part of the string to capitalize as well :). Come on, I know you can do it!!
  • westerwester Member Posts: 33
    Hi there.
    Yes, that was indeed very simple, but what if I want all starting-letters to become in uppercase like: "My Name" ? That means the first letter in every sentence + the one following a space? Is that just as simple?

    Thanks :wink:
  • DakkonDakkon Member Posts: 192
    edited 2004-10-19
    No, that's not nearly as simple:)
    That involves finding the beginning of each word to capitalize it
    You'd want to do something similar to the following:
    create variables CurValue, NewValue as text
    create variable Indx as integer
    create variable Cap as boolean
    IF (CurValue = '') THEN EXIT; //just to save a little extra needless processing in case of empty string
    
    Cap := TRUE; //Capitalize the first letter of the sentence regardless
    CurValue := LOWERCASE(CurValue); //convert everything to lowercase "in case" we have rogue uppercase letters
    
    FOR Indx := 1 TO STRLEN(CurValue) DO
    BEGIN
       IF (CurValue[Indx] = ' ') THEN
       BEGIN
          NewValue += ' '; //by adding a hardcoded space here we avoid having to waste processing on a call to Format()
          Cap := TRUE;
       END ELSE
       BEGIN
          IF Cap THEN
          BEGIN
             NewValue += UPPERCASE(FORMAT(CurValue[Indx]));
             Cap := FALSE;
          END ELSE
             NewValue += FORMAT(CurValue[Indx])
       END;
    END;
    
    EXIT(NewValue);
    

    I hope this helps :D
    If you are going to use this very often, I'd recommend putting it in a code unit as a method to call.
    Thad Ryker
    I traded my sanity for a railgun :mrgreen:
  • ajhvdbajhvdb Member Posts: 672
    Could you edit your message and put the code into a code tag?
  • DakkonDakkon Member Posts: 192
    Ok, reformatted my previous post, hope that's better:)
    Thad Ryker
    I traded my sanity for a railgun :mrgreen:
  • westerwester Member Posts: 33
    Hi Dakkon

    Nice function. It works just fine.

    Thank you very much :D
  • DakkonDakkon Member Posts: 192
    Glad it was helpfull :D
    Thad Ryker
    I traded my sanity for a railgun :mrgreen:
  • ara3nara3n Member Posts: 9,256
    One more thing I want to add, there is a property on fields called Title. If you set it to true it will automatically do this.

    The property only applies if the user manually enters the data. Through code you still have to do the parsing yourself.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • SavatageSavatage Member Posts: 7,142
    Using the code above to change all my item descriptions using a processing report.
    only a few additions when running it under a report
    OnAfterGetRecord()
    CLEAR(NewValue); //Added a clear to stop the constant appendg to the string
    WholeDescription := LOWERCASE(Item.Description +Item."Description 2")//We use both and combine to make 1
    IF (WholeDescription = '') THEN EXIT;
    Cap := TRUE;
    FOR indx := 1 TO STRLEN(WholeDescription) DO BEGIN
      IF (WholeDescription[indx] = ' ') THEN BEGIN
        NewValue += ' ';
        cap := TRUE;
       END ELSE BEGIN
        IF cap THEN BEGIN
          NewValue += UPPERCASE(FORMAT(WholeDescription[indx]));
          cap := FALSE;
         END ELSE BEGIN
          NewValue += FORMAT(WholeDescription[indx])
         END;
       END;
     END;
    Description := (COPYSTR(NewValue,1,30));
    "Description 2" := (COPYSTR(NewValue,31,30));
    MODIFY;
    EXIT;
    
Sign In or Register to comment.