Board index FlightGear Development Nasal

Capitalize string function  Topic is solved

Nasal is the scripting language of FlightGear.

Capitalize string function

Postby rleibner » Fri Jul 21, 2017 10:14 pm

Hi,
Does anyone know a function to capitalize the initial of each word in a sentence?
I mean, eg.:
Capit( "this is my plane" ) ==> "This Is My Plane"
Rodolfo
*************************
Non-shared knowledge is lost knowledge
User avatar
rleibner
 
Posts: 217
Joined: Fri May 19, 2017 7:17 pm
Location: Uruguay - SUMU
Callsign: CX-BEX
Version: 2180.2.0
OS: Ubuntu 17.10

Re: Capitalize string function

Postby Necolatis » Sat Jul 22, 2017 3:47 am

First use split on the string.

var strSplit = split(" ", str);

Then use substr to take out the first letter in each of the array elements. For example in a loop.

var initial = substr(strSplit[i],0,1);

Capitalize that initial

initial = string.uc(initial);

Do a loop to go through rest of the strings to reassemble the sentence. And add them together with space again.
There is probably more effective ways of doing it, but this way will work.
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore


Hangar: https://sites.google.com/site/fghangar/
User avatar
Necolatis
 
Posts: 1818
Joined: Mon Oct 29, 2012 12:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2018.1.1
OS: Windows 10 Pro

Re: Capitalize string function  

Postby rleibner » Sat Jul 22, 2017 3:51 am

Eureka !
Code: Select all
var capit = func(str) {
    var vec = split(" ",string.lc(str));
    for(var i=0;i<size(vec);i=i+1) {
       vec[i] = string.uc(left(vec[i],1)) ~substr(vec[i],1);
    }
   return string.join(" ",vec);
 }
Rodolfo
*************************
Non-shared knowledge is lost knowledge
User avatar
rleibner
 
Posts: 217
Joined: Fri May 19, 2017 7:17 pm
Location: Uruguay - SUMU
Callsign: CX-BEX
Version: 2180.2.0
OS: Ubuntu 17.10


Return to Nasal

Who is online

Users browsing this forum: No registered users and 1 guest