Raised This Month: $51 Target: $400
 12% 

Need help with a string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
-Striker-
Member
Join Date: Jun 2010
Old 07-22-2010 , 06:36   Need help with a string
Reply With Quote #1

Hello,

i have a integer. Now i want to create a new variable (string) like Integer*"#".

For example: Integer = 2 - Result = ##

I now only the way in php, with a for and .=.

Thank you for answers!
-Striker- is offline
neo_gis
Member
Join Date: Sep 2009
Location: Rabat
Old 07-22-2010 , 07:21   Re: Need help with a string
Reply With Quote #2

this should work

PHP Code:
new number value;

decl String:str[number 1];

for (new 
0numberi++)
{
    
strcat(strsizeof(str), "#");

__________________
neo_gis is offline
Send a message via MSN to neo_gis Send a message via Skype™ to neo_gis
-Striker-
Member
Join Date: Jun 2010
Old 07-22-2010 , 07:54   Re: Need help with a string
Reply With Quote #3

I try it when i am at home, thank you!
-Striker- is offline
Scone
Senior Member
Join Date: Apr 2010
Location: England
Old 07-22-2010 , 11:01   Re: Need help with a string
Reply With Quote #4

That's not really a very efficient way. StrCat has to search for the end of the string every time you call it. Also, the new string isn't null terminated (decl doesn't zero the contents of the memory), so it probably won't work at all.

Try this instead:

PHP Code:
decl String:str[number 1];

for(new 
0numberi++) str[i] = '#';

str[number] = 0
__________________
Scone is offline
Monkeys
Veteran Member
Join Date: Jan 2010
Old 07-22-2010 , 13:17   Re: Need help with a string
Reply With Quote #5

Why even bother using decl?
It's highly unlikely he's going to use a number even near the hundreds, so using new barely has any difference rather than being a lot less prone to bug.
__________________
Get a lid on that zombie,
he's never gonna be alri-i-ight.
Oooh get a lid on that zombie,
or he's gonna feed all night.
Monkeys is offline
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 07-22-2010 , 14:03   Re: Need help with a string
Reply With Quote #6

There's nothing wrong with decl as long as you do a quick initialization:

PHP Code:
str[0] = 0
However, in this case the most optimized solution would be something like this:

PHP Code:
new number 10;

// Create a uninitialized string.
decl String:str[number 1];

// Loop through all elements.
for (new 0<= numberi++)
{
    
str[i] = '#';
    
    
// Terminate the string if at the end.
    
if (== number)
    {
        
str[i] = 0;
    }

EDIT: Apparently that was exactly the same as Scone's solution. I didn't see that one. I did the termination check inside the loop, which is slightly slower.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)

Last edited by rhelgeby; 07-22-2010 at 14:09.
rhelgeby is offline
Send a message via MSN to rhelgeby
-Striker-
Member
Join Date: Jun 2010
Old 07-23-2010 , 06:18   Re: Need help with a string
Reply With Quote #7

ty
-Striker- is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 19:05.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode