Forum

> > CS2D > Scripts > string.sub
Forums overviewCS2D overview Scripts overviewLog in to reply

English string.sub

19 replies
To the start Previous 1 Next To the start

old string.sub

DragonAwper
User Off Offline

Quote
> My questions are:

• What string.sub exactly do?
• What string.len do?
• What tonumber do?
• What loadstring do?

I think loadstring is to load a variable in run-time

=========================
Look this: It's a kick Script
1
2
3
4
5
function saykick(id, txt)
     if(string.sub(txt,1,5)=="!kick") then
          parse("kick "..(tonumber(string.sub(txt,7,string.len(txt)))).." Kicked!")
     end
end

In this case why it's used?
> (txt,1,5)=="!kick")

× I don't understand why is: txt,1,5
I think is to return 5 letter form the text

Here too
> (txt,7,string.len(txt))
I think is to return 7 letter form the text

Am I correct?

I'm new in lua
And i don't know what these things exactly do.

Help-me
edited 2×, last 13.03.11 03:36:02 pm

old Re: string.sub

Banaan
User Off Offline

Quote
Ever considered taking a look at the Lua manual, PIL, other Lua tuts? Those are like the easiest basic functions in Lua. Google is your friend. http://lmgtfy.com/?q=lua+string.sub

Lua-users.org wiki has written
string.sub(s, i [, j])
s:sub(i [,j])

Return a substring of the string passed. The substring starts at i. If the third argument j is not given, the substring will end at the end of the string. If the third argument is given, the substring ends at and includes j.
1
2
3
4
5
6
7
8
9
10
> = string.sub("Hello Lua user", 7)      -- from character 7 until the end
    Lua user
    > = string.sub("Hello Lua user", 7, 9)   -- from character 7 until and including 9
    Lua
    > = string.sub("Hello Lua user", -8)     -- 8 from the end until the end
    Lua user
    > = string.sub("Hello Lua user", -8, 9)  -- 8 from the end until 9 from the start
    Lua
    > = string.sub("Hello Lua user", -8, -6) -- 8 from the end until 6 from the end
    Lua


Googling the rest ain't that hard...

http://lmgtfy.com/?q=lua+string.len
http://lmgtfy.com/?q=lua+tonumber
http://lmgtfy.com/?q=lua+loadlib

old Re: string.sub

DannyDeth
User Off Offline

Quote
string.sub = is a function called 'sub' in the wtring module. 'sub' is short for subsection, and that is what it does. It takes the subsection between the two last numbers and the variable supplied ( txt in banaan's case ).

old Re: string.sub

DragonAwper
User Off Offline

Quote
Good! It's help-me
And i'm not Wrong, Uhuw!

My area of ​​programming is Visual Basic and Delphi
I'm really good in C++ and C#
But in Lua I am a failure

√Thanks Banaan and DennyDeath

old Re: string.sub

DannyDeth
User Off Offline

Quote
Yasday has written
Lua is much easier than c++/c#..

Exactly. I double your statement.

Example of C/C++"
1
2
3
4
5
#include <stdio.h>
int main(void){
	printf("Hello World!\n");
	return 0;
}

Example of Lua:
1
print("Hello World!")

It even cut two extra chars off the last bit of "Hello World!\n" ( well, it would only print one character [ newline ], but yeah, in the code it was to chars
)

And don't make me go into memory management

old Re: string.sub

DragonAwper
User Off Offline

Quote
Now in Visual Basic

Sub Print()
text="Hello World!"
debug.print text
End Sub

Result: Hello World!

old Re: string.sub

DannyDeth
User Off Offline

Quote
Visual BASIC is a disgrace of a language. Never show code like that in this forum again! *Bans VB code for ever*

old Re: string.sub

DragonAwper
User Off Offline

Quote
kkkkk, ok.
Lua is cool, and now i know what is string.BláBlá

But tonumber still confused

Now what DragonAwper understand from this code?

1
2
3
4
5
function saykick(id, txt)
     if(string.sub(txt,1,5)=="!kick") then
          parse("kick "..(tonumber(string.sub(txt,7,string.len(txt)))).." Kicked!")
end
end

Line 2 : if 1-5 character == "kick" then
(look theres 5 chars.!) now it's explain why is txt 1,5

Line 3 : Kick the Number that's stay on the 7th space form the text to the text lenght,
And the reason is "Kicked!"

Am I right?
edited 2×, last 13.03.11 04:58:56 pm

old Re: string.sub

Flacko
User Off Offline

Quote
Yes you are.

The tonumber function is used to convert lua strings representing a number to an actual number.
For example: tonumber("5") returns 5.

You can't perform any arithmetical operations on a string representing a number, like addition, substraction, multiplication, etc, but you can do so if you convert it to a number.

However, in the code you posted here there's no need to use tonumber() because when the kicked player id is converted to a number it is then converted to a string again for concatenation.

If you read the Lua manual you'll see that this is called coercion, Lua converts a number to a string if needed when you're trying to concatenate it to a string. Moreover, Lua also converts a string to a number if you attempt any arithmetical operations on it.
For example: "5"+3 returns 8

old Re: string.sub

Danikah
User Off Offline

Quote
DragonAwper has written
Now in Visual Basic

Sub Print()
text="Hello World!"
debug.print text
End Sub

Result: Hello World!

Now in GML!
1
draw_text(x,y,"Hello World!")

But anyway, these answers helped me too alot.

old Re: string.sub

DragonAwper
User Off Offline

Quote
@Flacko
Great explanation! Now I understand better than before!
Thanks.

@Danikah
Very Good

@Topic
What is the best way to create a kick system?

Number 1
1
2
3
4
5
6
addhook("say","saykick")
function saykick(id, txt)
     if(string.sub(txt,1,5)=="!kick") then
          parse("kick "..(tonumber(string.sub(txt,7,string.len(txt)))).." Kicked!")
     end
end

Number 2
1
2
3
4
5
6
function saykick(id, t)
	if t:sub(1,5)=="!kick" then
		kid=tonumber(t:sub(7,8))
		parse("kick "..kid)
	end
end

This Question is very simply, but i want to see your Opinion

old Re: string.sub

KimKat
GAME BANNED Off Offline

Quote
What if the end is unknown?
So like...
1
2
x = 9 -- and further?
string.sub("Hello Lua user", 7,"..x)
Just wondering how to read after the 7th character...

old Re: string.sub

DragonAwper
User Off Offline

Quote
Eh, if have 32 players it's possible to kick, look:

7,8;
It's 2 Characters


0 - 9 = 1 char.
10 - 99 = 2 char

•About this i have 2 Replies:

> Yes I can create a Loop Structure with 1 to 32 but my examples are only "Examples"

> Yes I can use the String.Len too but my examples are only "Examples"

old Re: string.sub

KimKat
GAME BANNED Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
addhook("say","on_say")
function on_say(id,txt)
	slay = to.number(text[txt],1,7,8)
	if (txt=="!slay") then
		slay(id)
	end
end

for i = 1, 32 do
	text = string.sub(txt,i,"..string.len[i(s)]..")
	s = string.sub(s, 2)
	return s
end
What would this code do? I tried to understand it. I wanted it to do a check on how long the length it is on "i" lol, and thereafter limit it by just that from 1-32 it shouldn't go further than this... sort of like going through all these and seeing who said what from the say function in order to slay the id given. Maybe it's pathetic, but I really tried to understand this...

old Re: string.sub

Banaan
User Off Offline

Quote
1
2
local kid = t:match("%d+")
parse("kick " .. kid)

Note that this only works when there's just one id in there, as it will match each number in your string. (so !kick 23 will return 23, but !1 kick 23 will return 1)

old Re: string.sub

Flacko
User Off Offline

Quote
Ugh, people, better use Lee's string.split() function
1
2
3
4
5
6
7
8
function string.split(text,delimiter)
	local buff = {}
	delimiter = "[^"..(delimiter or " ").."]+"
	for w in string.gmatch(text,delimiter) do
		table.insert(buff,w)
	end
	return buff
end

This way you can easily split a text into tokens and access those words.
Example:
1
2
3
4
5
6
7
addhook("say","_say")
function _say(id,txt)
	txt = string.split(txt," ") --Txt is now a table storing it's words
	if txt[1] == "!kick" then --if the first word is kick
		parse("kick "..txt[2]) --We kick the player given in the second word
	end
end

Dealing with all those string.sub, string.len, etc causes me headache.

old Re: string.sub

KimKat
GAME BANNED Off Offline

Quote
@Flacko: Talking about headaches I had one earlier, lol. If I wanted to kick player 33 it would produce a Lua error that the player don't exist, so is there a way to ignore values over 32 or less than 1 let's say 0 (or even -1) though? is there a way to like "return 1"(ignore) it? as to not produce a Lua error. I like your script Flacko, it seems too be functional. So the second word being a number, would that be counted as is? In example id 2 or id 22 would read as is...

If so this script you did, would be quite epic and useful.

Ignore other values like this perhaps...
1
2
3
if txt[2]>=33 or txt[2]<=0 then
	return 1
end

old Re: string.sub

Danikah
User Off Offline

Quote
Er...
1
2
if player(txt[2],exists)
   parse("kick "..txt[2]) --We kick the player given in the second word
Maybe?

old Re: string.sub

Flacko
User Off Offline

Quote
Yes, would be something like that, but txt[2] is a string, not a number (as the player function espects)
So the right solution that I think you're looking for would be something like this:
1
2
3
4
5
6
7
8
9
10
addhook("say","_say")
function _say(id,txt)
	txt = string.split(txt," ") --Txt is now a table storing it's words
	if txt[1] == "!kick" then --if the first word is !kick
		if player(tonumber(txt[2]),"exists") then --and the given id is a player
			parse("kick "..txt[2]) --We kick the player given in the second word
		end
		return 1 --Don't display any text
	end
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview