inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,849
January 2015
msg
|
Post by Lynx on Jun 18, 2020 16:50:15 GMT -8
Is there a JavaScript equivalent to the old GWBASIC's: LEFT$; RIGHT$; and MID$?
Example:
Let LT$ = LEFT$("Tornado",3) // equals "Tor" Let RT$ = RIGHT$("Tornado",3) // equals "ado" Let MD$ = MID$("Tornado",3,3) // equals "rna"
"Tornado", in those examples, is also replaceable with a variable.
Let TT$ = "Tornado" Let LT$ = LEFT$(TT$,3) // = "Tor" etc.
Thanks!
|
|
inherit
Official Code Helper
65613
0
1
Oct 22, 2024 1:56:19 GMT -8
Chris
"'Oops' is the sound we make when we improve"
9,018
December 2005
horace
RedBassett's Mini-Profile
|
Post by Chris on Jun 19, 2020 3:47:51 GMT -8
The javascript String primitive has several methods to get this done: Let LT$ = LEFT$("Tornado",3) // equals "Tor"
let tt = "Tornado"; tt.substring(0,3) === "Tor" tt.substr(0,3) === "Tor" tt.slice(0,3) === "Tor"
Let RT$ = RIGHT$("Tornado",3) // equals "ado"
tt.substring(tt.length-3) == "ado"; tt.substr(-3) === "ado"; tt.slice(-3) === "ado"
Let MD$ = MID$("Tornado",3,3) // equals "rna"
tt.substring(2,5) === "rna" tt.substr(2,3) === "rna" tt.slice(2,5) === "rna"
|
|
inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,849
January 2015
msg
|
Post by Lynx on Jun 19, 2020 5:35:11 GMT -8
Thank you very much, Chris! I went to the link you posted (thank you very much again for that) and if I'm understanding correctly for the MID$ JS equivalents, substring and slice use the ending index (which yields the characters up to, but not including, the end index) as the 2nd parameter - whereas the substr uses the count (how many) from the starting index (including the starting index). Is that correct? So, if I wanted to get, for example (using let tt = "Tornado"), the "ornad" could be gotten by 1 of these: tt.substring(1,6)tt.substr(1,5)tt.slice(1,6)Do I have that correct? Thanks!
|
|
inherit
Official Code Helper
65613
0
1
Oct 22, 2024 1:56:19 GMT -8
Chris
"'Oops' is the sound we make when we improve"
9,018
December 2005
horace
RedBassett's Mini-Profile
|
Post by Chris on Jun 19, 2020 8:55:08 GMT -8
If you are using a browser with a built-in debugger then all you need to do is type it into the immediate window/console to see the result
"Tornado".substring(1,6)
Should immediately give you the result and remove any doubt, try any and all iterations
EDIT: It's been years since I've touched VB but I do recall you could use those methods to assign new characters (e.g. MID$("Tornado",3,3)="yyy" resulting in "Toyyydo") but not possible in JS, you would need to concatenate or splice.
|
|