padding string in left or right ?

Everything else that doesn't fall into one of the other PB categories.
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

padding string in left or right ?

Post by moricode »

from MSDN :
https://learn.microsoft.com/en-us/dotne ... es/padding

PadLeft
The String.PadLeft method creates a new string by concatenating enough leading pad characters to an original string to achieve a specified total length. The String.PadLeft(Int32) method uses white space as the padding character and the String.PadLeft(Int32, Char) method enables you to specify your own padding character.

The following code example uses the PadLeft method to create a new string that is twenty characters long. The example displays "--------Hello World!" to the console.

C#

Copy
string MyString = "Hello World!";
Console.WriteLine(MyString.PadLeft(20, '-'));

in PB , RSET from doc :

RSet()

Syntax

Result$ = RSet(String$, Length [, Character$])
Description

Pads a string to the right by adding extra characters to fit the specified length.

so , sometimes i always confuse , like driving a car , left or right it should call ?

thanks guy


// Moved from "Coding Questions" to "General Discussion" (Kiffi)
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

Re: padding string in left or right ?

Post by moricode »

MSDN:

Code: Select all

string MyString = "Hello World!";
Console.WriteLine(MyString.PadLeft(20, '-'));

output :
"--------Hello World!"


PureBasic:

Code: Select all


RSet("Hello World!", 20, "-" ) 

output:
"--------Hello World!"


This two function has the same output , one is call left in the Doc and the other one is call right in the Doc.

The command is name is in different direction too , Padleft vs Rset

Halve planet is in left and halve planet is in right and the are equal.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: padding string in left or right ?

Post by BarryG »

LSet() pushes the existing string to the left, and RSet() pushes the existing string to the right. Think of it like that.
normeus
Enthusiast
Enthusiast
Posts: 472
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: padding string in left or right ?

Post by normeus »

Both of those commands are doing padding, but the way they do it is different because of the verb "Pad" or "Set".


The method "PadLeft" is actually "Padding" the left side of the string.

in PureBasic "RSet" is "Setting" the string to the right.

I guess the Description in PureBasic's help file, to make it obvious, should read:

Sets a string to the right of the new string with specified length by adding extra characters to the left. ( a lot of words, easier to say padding)

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

Re: padding string in left or right ?

Post by moricode »

Every time when i want to put zero on left side, i have to look for the Right function (Rset) ,
when i want to put space on the right side i have to look for the Left function (Lset)

always i confuse and need to re-check the doc help to confirm which function is the one i need ,
not like MSDN . the function name is strict right away and understand,

not a big deal , may be is my brain problem
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: padding string in left or right ?

Post by Little John »

Just trying LSet() and RSet() will show the difference.
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

Re: padding string in left or right ?

Post by moricode »

Little John wrote: Tue Nov 07, 2023 7:24 am Just trying LSet() and RSet() will show the difference.
we know the difference , but when i want to do right side , i need to use LEFT function , so will make me halt and think twice always.
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: padding string in left or right ?

Post by Little John »

moricode wrote: Tue Nov 07, 2023 7:26 am we know the difference , but when i want to do right side , i need to use LEFT function , so will make me halt and think twice always.
E.g. LSet() sets the input string to the left side of the output string. That's all.
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

Re: padding string in left or right ?

Post by moricode »

Little John wrote: Tue Nov 07, 2023 7:33 am
moricode wrote: Tue Nov 07, 2023 7:26 am we know the difference , but when i want to do right side , i need to use LEFT function , so will make me halt and think twice always.
E.g. LSet() sets the input string to the left side of the output string. That's all.
yes , of course.

i had a text literal , and i want to put something on the left , so i use function PadLeft()

VS

i had a text literal , and i want to put something on the left , so i use function Rset() RightSet
the first one more understandable , the second one need a moment to think about.

which prefer ?
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: padding string in left or right ?

Post by Little John »

moricode wrote: which prefer ?
Neither of your two suggestions. I prefer the wording that I wrote in my previous message. Think of the situation like that, then it's straightforward.
The names of the functions are not LPad() and RPad().
User avatar
Demivec
Addict
Addict
Posts: 4269
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: padding string in left or right ?

Post by Demivec »

RSet() will right justify the string (with any needed padding on the left side).
LSet() will left justify the string (with any needed padding on the right side).
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: padding string in left or right ?

Post by mk-soft »

Nobody needs ...

Code: Select all


Macro PadLeft(_String_, _Len_, _Char_ = " ")
  RSet(_String_, _Len_, _Char_)
EndMacro

Macro PadRight(_String_, _Len_, _Char_ = " ")
  LSet(_String_, _Len_, _Char_)
EndMacro

a$ = PadLeft("Hello Word!", 20, "-")
Debug a$
a$ = PadRight("Hello Word!", 20, "-")
Debug a$

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: padding string in left or right ?

Post by Oso »

I feel that LSet() and RSet() are perfectly well-defined, while being unrelated to the padding function in the other language referred to. LSet() and RSet() happen to correspond almost exactly with equivalent functions in other Basic languages I've used, which are focussed very much on string handling and financial data.

The only thing I felt was a disappointment, is that where the string to be formatted is longer than the specified length, RSet() and LSet() behave in the same way, which is that they both truncate the right side, instead of RSet() more logicially giving precidence to the right side. I was surprised by this and it is an inconvenience when dealing with financials, because it's then necessary to use Right(string, length) first.

Code: Select all

a.s = "320.85"
Debug Chr(34) + RSet(a.s, 10) + Chr(34)               ; "    320.85"

a.s = "1234567890.23"
Debug Chr(34) + RSet(a.s, 10) + Chr(34)               ; "1234567890" - Not okay for financials

Debug Chr(34) + RSet(Right(a.s, 10), 10) + Chr(34)    ; "4567890.23" - Okay
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: padding string in left or right ?

Post by Tenaja »

You are very new here. The RSet and LSet commands are over 20 years old, and appear to be modelled after vb's commands of the same name.

Any time you are coming from a different language, you will have a learning curve. In the beginning, I created many macros to "fix" pb into what i was accustomed to. However, after a couple months, I realized it was much better to embrace the language as it is, and stop that silliness. That mind shift certainly made example code and forum code much more logical to read.

Hopefully you will grow accustomed to PB and embrace it too, as it makes life much easier.

Have fun!
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: padding string in left or right ?

Post by AZJIO »

Tenaja wrote: Tue Nov 07, 2023 3:39 pm In the beginning, I created many macros to "fix" pb into what i was accustomed to.
I also tried to create functions that I was used to
Post Reply