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 11: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: 269
Joined: Fri May 19, 2017 8:17 pm
Location: Uruguay - SUMU
Callsign: CX-BEX
Version: next
OS: Ubuntu 18.04.4

Re: Capitalize string function

Postby Necolatis » Sat Jul 22, 2017 4: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
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: Capitalize string function  

Postby rleibner » Sat Jul 22, 2017 4: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: 269
Joined: Fri May 19, 2017 8:17 pm
Location: Uruguay - SUMU
Callsign: CX-BEX
Version: next
OS: Ubuntu 18.04.4


Return to Nasal

Who is online

Users browsing this forum: No registered users and 3 guests