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

Getting a string at a certain index


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
necavi
Veteran Member
Join Date: Sep 2010
Old 11-03-2011 , 04:20   Getting a string at a certain index
Reply With Quote #1

I can't seem to get just the character at a certain index (if I use randomstr[2] it'll give me everything from the third character onwards) there has to be a way to do this, I simply haven't figured it out yet, despite looking all through the String docs.
necavi is offline
Scone
Senior Member
Join Date: Apr 2010
Location: England
Old 11-03-2011 , 05:14   Re: Getting a string at a certain index
Reply With Quote #2

Here's a function from my calculator plugin. It puts the character into a new integer, then works with that integer value. Hope that helps.

PHP Code:
ReadToken(const String:source[], &pos, &Float:num, &op) {

    new 
source[pos]; // This is the relevant line
    
if(== 0) return 0;
    
    
decl String:tmp[20];
    
    if(
IsCharNumeric(c) || == '.' || == '_') {
        new 
0;
        while((
source[pos]) != && sizeof(tmp)-&& (IsCharNumeric(c) || == '.' || == '_')) {
            if(
== '_''-';
            
tmp[i++] = c;
            
pos++;
        }
        if(
sizeof(tmp)-3) return -1;
        
tmp[i] = 0;
        
num StringToFloat(tmp);
        return 
2;
    }
    
    if(
IsCharAlpha(c)) {
        new 
0;
        while((
source[pos]) != && sizeof(tmp)-&& IsCharAlpha(c)) {
            
tmp[i++] = c;
            
pos++;
        }
        if(
sizeof(tmp)-3) return -1;
        if(
!= '(') return -2;
        
tmp[i] = 0;
        
op FindFunc(tmp);
        return 
3;
    }
    
    
op c;
    
pos++;
    return 
1;

__________________
Scone is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 11-03-2011 , 05:21   Re: Getting a string at a certain index
Reply With Quote #3

Calculator plugin? That's exactly what I was going to do! XD


Edit: Ahhhh, requiring spaces, I was trying to avoid that (but it seems as if it would be far too hard without them)

Last edited by necavi; 11-03-2011 at 05:22.
necavi is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 11-03-2011 , 06:26   Re: Getting a string at a certain index
Reply With Quote #4

Addendum: I'm trying to adapt that to a native for use in multiple plugins, but unfortunately I haven't figured out how to return a value on only one return (the compiler doesn't like it) but I don't think I can give it any value to indicate that the plugin failed to complete its task.
necavi is offline
Scone
Senior Member
Join Date: Apr 2010
Location: England
Old 11-03-2011 , 09:38   Re: Getting a string at a certain index
Reply With Quote #5

Sorry, I don't fully understand the question.
__________________
Scone is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 11-03-2011 , 18:33   Re: Getting a string at a certain index
Reply With Quote #6

Quote:
Originally Posted by necavi View Post
I can't seem to get just the character at a certain index (if I use randomstr[2] it'll give me everything from the third character onwards) there has to be a way to do this, I simply haven't figured it out yet, despite looking all through the String docs.
randomstr[2] just is one character, however if you use it with string functions it will be used as pointer and returns the whole rest string of course.
don't use it with string functions.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 11-04-2011 , 17:49   Re: Getting a string at a certain index
Reply With Quote #7

Quote:
Originally Posted by berni View Post
randomstr[2] just is one character, however if you use it with string functions it will be used as pointer and returns the whole rest string of course.
don't use it with string functions.
Which is why you should strcopy it if you plan on doing further operations on it.

Keep in mind that a 1-character string still needs to be two characters long:
PHP Code:
decl String:newString[2];
strcopy(randomstr[2], newString); 
(assuming I have the right syntax for strcopy)
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
McFlurry
Veteran Member
Join Date: Mar 2010
Location: RemoveEdict(0);
Old 11-04-2011 , 18:01   Re: Getting a string at a certain index
Reply With Quote #8

Quote:
Originally Posted by Powerlord View Post
PHP Code:
decl String:newString[2];
strcopy(randomstr[2], newString); 
(assuming I have the right syntax for strcopy)
maxlength is missing. and destination string is first. unless randomstr was suppose to be the destination.

PHP Code:
decl String:newString[2];
strcopy(newStringsizeof(newString), randomstr[2]); 
A 1 character string should be [1], no? 0 is the valid string pointer and 1 is the null terminator.
__________________

Last edited by McFlurry; 11-04-2011 at 18:03.
McFlurry is offline
Send a message via Skype™ to McFlurry
Scone
Senior Member
Join Date: Apr 2010
Location: England
Old 11-04-2011 , 18:06   Re: Getting a string at a certain index
Reply With Quote #9

The [2] in the declaration implies that there are two elements, [0] and [1]. Declaring it with [1] element would mean that there was only one, [0].

If you're only working with one character there's no need to declare a string, just use a single integer.
__________________
Scone is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 11-04-2011 , 20:37   Re: Getting a string at a certain index
Reply With Quote #10

Quote:
Originally Posted by Scone View Post
If you're only working with one character there's no need to declare a string, just use a single integer.
This.
Just store it in a cell directly.
__________________
asherkin 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 10:42.


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