Raised This Month: $32 Target: $400
 8% 

Splitting strings after a specifik number of characters is found


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
striker07
Veteran Member
Join Date: Mar 2012
Location: Solar System/Earth/Belgi
Old 04-27-2014 , 22:04   Splitting strings after a specifik number of characters is found
Reply With Quote #1

Anybody knows a native or stock to split strings after an amount of characters inside the string is met?

fe i have a string with maxlength of 512, I want to split up that string in strings of 128th characters, so that in this case i would have 4 strings with 128 chars in it.

Can't find a native in the Strings include that can do this
__________________

Working on:
[CSGO/CSS] Mmorpg - an extensive XP/level modulair platform
Progress: [♣♣♣♣♣♣♣|♣♣♣]
striker07 is offline
arthurdead
Senior Member
Join Date: Jul 2013
Old 04-27-2014 , 22:11   Re: Splitting strings after a specifik number of characters is found
Reply With Quote #2

Here also instead of looking in the includes use this
arthurdead is offline
striker07
Veteran Member
Join Date: Mar 2012
Location: Solar System/Earth/Belgi
Old 04-27-2014 , 22:58   Re: Splitting strings after a specifik number of characters is found
Reply With Quote #3

mmmm correct me if i'm wrong but this will split a string by searching a match with a substring(split)

what i need is a similiar function but instead of split being a string it needs to be an integer to specify at wich character to split the string.

- Thats what i used to check the strings include ;)
__________________

Working on:
[CSGO/CSS] Mmorpg - an extensive XP/level modulair platform
Progress: [♣♣♣♣♣♣♣|♣♣♣]
striker07 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-28-2014 , 01:41   Re: Splitting strings after a specifik number of characters is found
Reply With Quote #4

For most languages, this would be done with repeated calls to the substring command... but SourcePawn doesn't have such a command.

However, due to how arrays work (SourcePawn Strings are arrays), you can use strcopy or Format to split strings into pieces... mainly by passing the String variable with an offset (i.e. myString[128] to start at the 129th character).

One catch: All strings have a null terminator in SourcePawn. So, to divide a 512 character string into 4 parts, the parts need to be 129 characters each, NOT 128.

It's late and I don't have time to do an example of how to do this right now.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 04-28-2014 at 01:42.
Powerlord is offline
striker07
Veteran Member
Join Date: Mar 2012
Location: Solar System/Earth/Belgi
Old 04-28-2014 , 10:47   Re: Splitting strings after a specifik number of characters is found
Reply With Quote #5

ah yes right, gonna try some things tonight to see if i can get it to work.
__________________

Working on:
[CSGO/CSS] Mmorpg - an extensive XP/level modulair platform
Progress: [♣♣♣♣♣♣♣|♣♣♣]
striker07 is offline
striker07
Veteran Member
Join Date: Mar 2012
Location: Solar System/Earth/Belgi
Old 04-28-2014 , 18:51   Re: Splitting strings after a specifik number of characters is found
Reply With Quote #6

hmmm loveley, works like a charm
I might make a native out of this, still can't believe sourcemod doesnt have a function like this already :s

PHP Code:
        decl String:LvlDescr[512];
    
decl String:BackupDescr[2][512];
    
decl String:LongDescr[4][129];
        
        
Mmorpg_GetLevelDescription(lvlidLvlDescr512);
    new 
DescrLength strlen(LvlDescr);
    if( 
DescrLength )
    {
        
Format(LvlDescr512"No description provided");
    }
    else if( 
DescrLength 128 )
    {
        new 
iStrParags RoundToCeil(float(DescrLength)/128);
        
        for(new 
strstr<iStrParagsstr++)
        {
            if( 
str == )
            {
                
Format(LongDescr[str], 129LvlDescr);
            
                
ExplodeString(LvlDescrLongDescr[str], BackupDescr2512true);
            }
            else {
                
Format(LongDescr[str], 129BackupDescr[1]);
                
                
ExplodeString(BackupDescr[1], LongDescr[str], BackupDescr2512true);
            }
        }
        switch( 
iStrParags )
        {
            case 
2: { Format(LvlDescr512"%s\n%s"LongDescr[0], LongDescr[1]); }
            case 
3: { Format(LvlDescr512"%s\n%s\n%s"LongDescr[0], LongDescr[1], LongDescr[2]); }
            case 
4: { Format(LvlDescr512"%s\n%s\n%s\n%s"LongDescr[0], LongDescr[1], LongDescr[2], LongDescr[3]); }
        }
    } 
__________________

Working on:
[CSGO/CSS] Mmorpg - an extensive XP/level modulair platform
Progress: [♣♣♣♣♣♣♣|♣♣♣]

Last edited by striker07; 04-28-2014 at 18:52.
striker07 is offline
Reply


Thread Tools
Display Modes

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 14:51.


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