PtokaX forum

Development Section => HOW-TO's => Topic started by: plop on 17 December, 2003, 02:36:47

Title: HOW-TO: strsub
Post by: plop on 17 December, 2003, 02:36:47
strsub(string, start, [end])

strsub substracts a string from a string, from "start" to "end" (if end is not given it uses the end of the input string as end, -1)
in lua the first character in the string gets the value 1, if you want to count from the end of the string you have to use negative vallue's (-1 is the last character of the string).
so if you for example want to strip the first end last character of a string then strsub(string, 2, -2) does what you want.

start the next script with the command line lua.exe.
it gives a simple example of what strsub does.
when launched "start" and "end" are both 1, in the first loop "end" is increased by 1 every step.
the second increases "start" by 1 every step.
and as last it strippes the first and last character.

(strlen(string) returns the lenght of the given string.)
text = "test string"
start = 1
End = 1

for i=1,strlen(text) do
   string2 = strsub(text, start, End)
   print(string2)
   End = End +1
end

for i=1,strlen(text) do
   string2 = strsub(text, start, End)
   print(string2)
   start = start + 1
end

print(strsub(text, 2, -2))

this is the result.
t
te
tes
test
test
test s
test st
test str
test stri
test strin
test string
test string
est string
st string
t string
 string
string
tring
ring
ing
ng
g
est strin

plop
Title:
Post by: kepp on 18 December, 2003, 01:47:05
Hmm, i know i've seen this before, and i played with it :)

Thanks plop, That sure helped me :D
Title:
Post by: plop on 18 December, 2003, 02:25:19
QuoteOriginally posted by kepp
Hmm, i know i've seen this before, and i played with it :)

Thanks plop, That sure helped me :D
yep, posted it before on a question from you.
was a good example just i added some more.

plop