Padding a string with spaces - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: Padding a string with spaces (/thread-3238.html) |
Padding a string with spaces - Joe Horn - 03-02-2015 07:20 AM 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. RE: Padding a string with spaces - bobkrohn - 03-03-2015 07:32 AM 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:
RE: Padding a string with spaces - Angus - 03-03-2015 08:02 AM 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. RE: Padding a string with spaces - Han - 03-03-2015 05:08 PM (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? RE: Padding a string with spaces - bobkrohn - 03-03-2015 09:34 PM Home View (testing) and in Programming environment primarily. I'm not using CAS so am tone deaf regarding subtle differences. RE: Padding a string with spaces - Mark Hardman - 03-03-2015 10:37 PM (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. RE: Padding a string with spaces - Mark Hardman - 03-03-2015 10:40 PM (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). RE: Padding a string with spaces - bobkrohn - 03-04-2015 02:20 AM (03-03-2015 10:40 PM)Mark Hardman Wrote: It looks like this can be used to generate a left padded string by swapping the arguments: Wished I thought of that. Good going! I only hope these tidbits show up in the next version of the User Guide. RE: Padding a string with spaces - bobkrohn - 03-04-2015 03:30 AM 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:
RE: Padding a string with spaces - Mark Hardman - 03-04-2015 04:09 AM (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. RE: Padding a string with spaces - Angus - 03-04-2015 05:43 AM 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. RE: Padding a string with spaces - bobkrohn - 03-04-2015 06:11 AM (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) RE: Padding a string with spaces - StephenG1CMZ - 10-07-2015 10:02 PM 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:
RE: Padding a string with spaces - bobkrohn - 10-09-2015 07:58 AM I like it! |