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

Counting substrings in string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sebxx4
Senior Member
Join Date: Feb 2013
Old 05-16-2022 , 07:29   Counting substrings in string
Reply With Quote #1

Hello,
How can I count substrings in string? Eg. I have a string: "line1^nline2^nline3". Now, I want to count amount of "^n" in it. I didn't find any function to do this.
sebxx4 is offline
Supremache
Veteran Member
Join Date: Sep 2019
Location: Egypt
Old 05-16-2022 , 09:29   Re: Counting substrings in string
Reply With Quote #2

If I understand correctly this is what you want..

PHP Code:
public Function()
{
    new 
szArg64 ], iCount;
    
formatexszArgcharsmaxszArg ), "Hey^nDude^nWelcome^nto^nAlliedmods" )
    while( 
containiszArg"^n" ) != )
    {
        
iCount++;
    }
    
client_print(idprint_chat,  "New lines counts %i"iCount )    

__________________
Youtube.com/Supremache

Bank System [Nvault - SQL Support]
VIP System
  • If you think it's that simple, then do it yourself.
Supremache is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 05-16-2022 , 16:12   Re: Counting substrings in string
Reply With Quote #3

containi returns -1 when no match is found, not 0.

Code:
/**
 * Tests whether a string is found inside another string.
 *
 * @param source		String to search in.
 * @param string		Substring to find inside the original string.
 *
 * @return				-1 on failure (no match found). Any other value
 *						indicates a position in the string where the match starts.
 */
__________________









Last edited by CrazY.; 05-16-2022 at 16:13.
CrazY. is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-16-2022 , 23:10   Re: Counting substrings in string
Reply With Quote #4

Supremache, doing that (with the correct not found value) will cause an infinite loop.

I found a version of this function in Exolent's string stocks here. I've never used it before but I suspect that it'll work well enough for you.
__________________

Last edited by fysiks; 05-16-2022 at 23:38.
fysiks is offline
sebxx4
Senior Member
Join Date: Feb 2013
Old 05-17-2022 , 02:51   Re: Counting substrings in string
Reply With Quote #5

Thanks
sebxx4 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 18:35.


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