Formatting string

wester
Member Posts: 33
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?
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?

0
Comments
-
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 railgun0 -
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!!
0 -
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?
Thanks0 -
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 booleanIF (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
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 railgun0 -
Could you edit your message and put the code into a code tag?0
-
Ok, reformatted my previous post, hope that's better:)Thad Ryker
I traded my sanity for a railgun0 -
Hi Dakkon
Nice function. It works just fine.
Thank you very much0 -
Glad it was helpfullThad Ryker
I traded my sanity for a railgun0 -
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.0 -
Using the code above to change all my item descriptions using a processing report.
only a few additions when running it under a reportOnAfterGetRecord() 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;
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions