Padding a string with spaces
|
03-02-2015, 07:20 AM
(This post was last modified: 03-02-2015 07:56 AM by Joe Horn.)
Post: #1
|
|||
|
|||
Padding a string with spaces
Here's an unexpected hidden feature. To pad a short string with spaces so that its total length is X characters, all it takes is:
REPLACE(string,X+1,"") where "" is an empty string. If the input string is already X characters long (or longer), it is returned unchanged. Spaces are only appended if SIZE(string)<X. Examples: REPLACE("ABCDE",10+1,"") --> "ABCDE " (ten characters long) REPLACE("ABCDEFGHIJKL",10+1,") --> "ABCDEFGHIJKL" (unchanged) Warning: Ridiculously large values for X makes Prime unhappy. <0|ɸ|0> -Joe- |
|||
03-03-2015, 07:32 AM
(This post was last modified: 03-03-2015 07:35 AM by bobkrohn.)
Post: #2
|
|||
|
|||
RE: Padding a string with spaces
This is the useful type stuff I love to read about.
Serendipity is a wonderful thing. But you have to be able to recognize a good thing when it appears. Good for you, Thanks I now have a new Function in my Library. Code:
|
|||
03-03-2015, 08:02 AM
Post: #3
|
|||
|
|||
RE: Padding a string with spaces
Maybe it is important to note that I have to call
replace(). It is capitalized by the prime, but using REPLACE() directly won't work for me. |
|||
03-03-2015, 05:08 PM
Post: #4
|
|||
|
|||
RE: Padding a string with spaces
(03-03-2015 08:02 AM)Angus Wrote: Maybe it is important to note that I have to call Were you using REPLACE() in the CAS or Home view? Graph 3D | QPI | SolveSys |
|||
03-03-2015, 09:34 PM
Post: #5
|
|||
|
|||
RE: Padding a string with spaces
Home View (testing) and in Programming environment primarily.
I'm not using CAS so am tone deaf regarding subtle differences. |
|||
03-03-2015, 10:37 PM
(This post was last modified: 03-03-2015 10:57 PM by Mark Hardman.)
Post: #6
|
|||
|
|||
RE: Padding a string with spaces
(03-03-2015 08:02 AM)Angus Wrote: Maybe it is important to note that I have to call This only works in algebraic mode. Most likely you are in RPN mode. In that case you will need to enter the following on the stack: "ABCDE" 11 "" Then enter: REPLACE(3) --- ETA: Alternately, you could enter 'REPLACE("ABCDE",10+1,"")' followed by EVAL to get the result in RPN mode. Ceci n'est pas une signature. |
|||
03-03-2015, 10:40 PM
(This post was last modified: 03-04-2015 04:22 AM by Mark Hardman.)
Post: #7
|
|||
|
|||
RE: Padding a string with spaces
(03-02-2015 07:20 AM)Joe Horn Wrote: Here's an unexpected hidden feature. To pad a short string with spaces so that its total length is X characters, all it takes is: REPLACE("",X+1,string) For example: Code:
ETD: This doesn't work as well as first though (see below). Ceci n'est pas une signature. |
|||
03-04-2015, 02:20 AM
(This post was last modified: 03-04-2015 02:21 AM by bobkrohn.)
Post: #8
|
|||
|
|||
RE: Padding a string with spaces | |||
03-04-2015, 03:30 AM
(This post was last modified: 03-04-2015 03:43 AM by bobkrohn.)
Post: #9
|
|||
|
|||
RE: Padding a string with spaces
Just noticed some thing about the "Left Padding".
Appears that the Right Padding fits your String into a block of spaces that you designate. However, the Left Padding just ADDS these spaces in front of the String. I was trying to make a "do-all" function and that's why I noticed. Unless of course I may be doing something wrong. I'm pretty sure the Centering one is wrong but I quit when I noticed the above discrepancy. Ideas anyone? Maybe just subtract the length of your String from the number of desired Left pads? Code:
|
|||
03-04-2015, 04:09 AM
(This post was last modified: 03-04-2015 04:24 AM by Mark Hardman.)
Post: #10
|
|||
|
|||
RE: Padding a string with spaces
(03-04-2015 03:30 AM)bobkrohn Wrote: However, the Left Padding just ADDS these spaces in front of the String. Sorry about that. I've put strike-through on my previous post. I agree that you will need to set the number of characters to pad. The problem is that the offset required doesn't seem consistent. targetLength - stringLength - ? I appears that this left pad "trick" is less useful than I thought. Ceci n'est pas une signature. |
|||
03-04-2015, 05:43 AM
(This post was last modified: 03-04-2015 06:05 AM by Angus.)
Post: #11
|
|||
|
|||
RE: Padding a string with spaces
fyi: I have home in RPN but tried in CAS. The error is "Error: Bad Argument Type" which I also get when in home/alg or home/textbook. replace() works. Interessting.
|
|||
03-04-2015, 06:11 AM
(This post was last modified: 03-04-2015 08:57 AM by bobkrohn.)
Post: #12
|
|||
|
|||
RE: Padding a string with spaces
(03-04-2015 04:09 AM)Mark Hardman Wrote: Sorry about that. I've put strike-through on my previous post. What's to be sorry about!? Your exhibiting "thinking outside the box" which is a G-O-O-D thing. I will still fiddle around with it as I hope others will too. Have fun. Here's what I came up with so far. PHP Code: EXPORT PadLR(MyStr,MyLen,LorR) |
|||
10-07-2015, 10:02 PM
(This post was last modified: 10-07-2015 10:09 PM by StephenG1CMZ.)
Post: #13
|
|||
|
|||
RE: Padding a string with spaces
I decided to have a go at this, based on the original syntax.
My changes include: An input guard against negative/fractional parameters. The input string is not modified. In the centre case, a recursive call is used which I think makes the logic clearer, and avoids repeatedly adding spaces one by one. My implementation is probably less efficient, but provides a comparison for the previous logic. Code:
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
10-09-2015, 07:58 AM
Post: #14
|
|||
|
|||
RE: Padding a string with spaces
I like it!
|
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)