Saturday, January 16, 2021

#1 2021-01-14 11:49:44 am

Headroom San
Member
From:: Stratford upon Avon
Registered: 2016-07-15
Posts: 42

Adding and saving whitespace/space characters to words

Dear all,

Thanks for looking.

I need to add spaces or blanks to individual 'source' words.
Each word HAS to be a given length - 20 characters. The client system can only accept fixed length entries.

When a word is shorter or longer than required, the entry is rejected. I have been able to trim words that are too long.

So here is an example ;
"HEADROOM/SAN" is 12 characters long
what is needed is
"HEADROOM/SAN        " which is 20 characters long.

Extensive Googling, reading the Apple manual and so on have not thrown up a guide, or at least I have been unable to find one.

Can anybody suggest where I might look?

Thanks for reading this.

Max


Beware the beast Man, for he is the Devil's pawn. Alone among God's primates, he kills for sport or lust or greed. Yea, he will murder his brother to possess his brother's land. Let him not breed in great numbers, for he will make a desert of his home and yours. Shun him; drive him back into his jungle lair, for he is the harbinger of death.


Filed under: space, whitespace

Offline

 

#2 2021-01-14 12:18:31 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11714
Website

Re: Adding and saving whitespace/space characters to words

This is a simple handler. It appends the character until the desired length is reached

Applescript:

on pad(theText, theCharacter, maxLength)
   repeat while length of theText < maxLength
       set theText to theText & theCharacter
   end repeat
   return theText
end pad

set testString to "HEADROOM/SAN"
set testString to pad(testString, space, 20)


regards

Stefan

Offline

 

#3 2021-01-14 12:32:49 pm

Headroom San
Member
From:: Stratford upon Avon
Registered: 2016-07-15
Posts: 42

Re: Adding and saving whitespace/space characters to words

Stefan,

Perfect and really simple/compact.

I was unaware I could 'declare/use' space. I was trying to use %20 or " " and so on.
set testString to pad(testString, space, 20)

Thanks for your trouble.

Max


Beware the beast Man, for he is the Devil's pawn. Alone among God's primates, he kills for sport or lust or greed. Yea, he will murder his brother to possess his brother's land. Let him not breed in great numbers, for he will make a desert of his home and yours. Shun him; drive him back into his jungle lair, for he is the harbinger of death.

Offline

 

#4 2021-01-14 12:42:18 pm

robertfern
Member
Registered: 2011-11-29
Posts: 92

Re: Adding and saving whitespace/space characters to words

Heres a version that doesn't have a loop

Applescript:


on pad(theText)
   set c to length of theText
   if c ≥ 20 then return
   set c to 20 - c
   return theText & text 1 thru c of " " -- the string is made up of 20 spaces
end pad

set testString to "HEADROOM/SAN"
set testString to pad(testString)

It might be faster, but the spaces are hard-coded

**EDIT - removed the passed "maxLength" variable since I hard-coded it for length of 20

Last edited by robertfern (2021-01-14 04:24:30 pm)

Offline

 

#5 2021-01-14 03:16:51 pm

Headroom San
Member
From:: Stratford upon Avon
Registered: 2016-07-15
Posts: 42

Re: Adding and saving whitespace/space characters to words

Hello Robert,

Thanks for your script too. I have used Stefan's script because the loop allows me to add a couple of other operations within the same loop.

As always happens, you and Stefan have both taught me something new.

Thanks for sending it over.


Max (Headroom San)


Beware the beast Man, for he is the Devil's pawn. Alone among God's primates, he kills for sport or lust or greed. Yea, he will murder his brother to possess his brother's land. Let him not breed in great numbers, for he will make a desert of his home and yours. Shun him; drive him back into his jungle lair, for he is the harbinger of death.

Offline

 

#6 2021-01-14 04:51:30 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 193

Re: Adding and saving whitespace/space characters to words

Another very simple way is to concatenate the text string with a sequence of spaces, before or after, and then take the left or right desired number of characters. For example:

Applescript:



set targetLength to 20
set thePad to " " -- 19 spaces
set testString to "HEADROOM/SAN"
set finalTestString to (testString & thePad)

set finalResult to text 1 thru targetLength of finalTestString

--return finalResult --> "HEADROOM/SAN "
--return the number of characters of finalResult --> 20

--Padding a number with leading zeroes
--Example: leading zeroes to pad to three places

Applescript:


set theLeadingPad to "000"
set testNum to "7"

set finalNumber to text from -3 to -1 of (theLeadingPad & testNum)

--return finalNumber --> "007"

Last edited by kerflooey (2021-01-14 04:53:38 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)