[solved] Length of a string ?

You can talk about ManiaScript for ManiaPlanet here

Moderator: English Moderator

Post Reply
User avatar
gouxim
Nadeo
Nadeo
Posts: 1186
Joined: 14 Jun 2010, 17:20

[solved] Length of a string ?

Post by gouxim »

I have a string. How to know its length?

Edit 2: you can use TextLib::Length() since the last update!

Edit: here's how to do it

Code: Select all

#Include "TextLib" as TextLib

Integer manialib_strlen(Text text)
{
	declare length = 0;
	while(TextLib::SubString(text, length,1) != "")
	{
		length = length+1;
	}
	return length;
}
Please do not PM for support. Instead, create a thread so that everyone can contribute or benefit from the answer! 8-)
konte
Posts: 236
Joined: 15 Jun 2010, 12:36
Location: Germany

Re: Length of a string ?

Post by konte »

Here's my solution:

Code: Select all

Integer strlen(Text text) {
	for (i, 0, 100000)
		if (TextLib::SubString(text, i, 1) == "") return i;
	return 0;
}
Yes, this solution is crappy, but I don't think there's any other way :D
User avatar
gouxim
Nadeo
Nadeo
Posts: 1186
Joined: 14 Jun 2010, 17:20

Re: Length of a string ?

Post by gouxim »

I talked to gugli and there's no such function at the moment. Before reading your message I came up with a similar solution, but using a while loop so there's no upper bound for the size:

Code: Select all

#Include "TextLib" as TextLib

Integer manialib_strlen(Text text)
{
	declare length = 0;
	while(TextLib::SubString(text, length,1) != "")
	{
		length = length+1;
	}
	return length;
}
Please do not PM for support. Instead, create a thread so that everyone can contribute or benefit from the answer! 8-)
konte
Posts: 236
Joined: 15 Jun 2010, 12:36
Location: Germany

Re: Length of a string ?

Post by konte »

I agree, a while loop is the better solution :)
User avatar
Slig
Posts: 637
Joined: 15 Jun 2010, 11:52
Location: TraXicoLand

Re: Length of a string ?

Post by Slig »

[quote="gouxim"]I talked to gugli and there's no such function at the moment[quote]
then perhaps it would be better to add it, wouldn't it ? a loop with a substring seems a very ugly solution (despite the fact that it seems to be the only one actually)
User avatar
Gugli
Nadeo
Nadeo
Posts: 536
Joined: 14 Jun 2010, 17:35
Location: Mon PC est à Paris, mais mon coeur rode dans les forêts de Lozère

Re: Length of a string ?

Post by Gugli »

Slig wrote:then perhaps it would be better to add it
Well, of course. In fact it's already added. I will try to see if I can slip this in next update.
--
(>~_~)> ═╦═ ╔╦╗ <(~_~<)
Post Reply

Return to “ManiaScript”

Who is online

Users browsing this forum: No registered users and 1 guest