This site has been archived and made available for preservation purposes. No edits can be made.

Results 1 to 2 of 2

Thread: ReplaceChars Function

  1. #1

    ReplaceChars Function


    /*
    * Replaces characters in a string
    * Usage: strreplace(string, 'Input Text', 'Replacing Text');
    */

    This function is quite handy for me, and I've been using it for a while.

    Code :
    stock replacechars(string[], match, replace)
    {
      for(new i=0; string[i]; i++)
      {
        if(string[i] == match)
        {
          string[i] = replace;
        }
      }
    }

    Enjoy.


    I just come here to view whether there has been any progress or not, and review the wonderful progress that Recycler and the development team has made.
    My sincere thanks goes to them and to others who value my contributions the community, not to mention the ones that solely contribute.

  2. #2

    Re: ReplaceChars Function

    Code :
    public ReplaceCharacter(string[], match, replace)
    {
    	new iWrite = strlen( string ), count;
    	do
    	{
    	  if( string[count] == match )
          string[count] = replace;
    	}
    	while( iWrite < count++ );
    }

    Your code doesn't do what you said, it'll just replace the character in the instant I.

    If you had a roleplay name and you'd want to remove the underscore off the name like: Jack_London, this would be the appropriate thing to use (or things).

    These can be used as well:
    Code :
    #if !defined isnull
    	#define isnull(%1) (((%1[0]) == '\0') || (((%1[0]) == '\1') && ((%1[1]) == '\0')))
    #endif
     
    /**
     * Replace a substring in a string
     * @param	string		The source string
     * @param	find		The substring to be replaced
     * @param	replace		The substring to replace with
     * @param	ignorecase	If the search is case insensitive
     * @param	count		The number of substrings to be replaced
     * @param	maxlength	The length of the source string
     * @return	The number of substrings replaced
     */
    stock strreplace(string[], const find[], const replace[], bool:ignorecase=false, count=cellmax, maxlength=sizeof(string))
    {
    	if(isnull(find) || isnull(string)) return 0;
    	new matches;
    	for(new idx, flen = strlen(find), rlen = strlen(replace), pos = strfind(string, find, ignorecase); pos != -1 && idx < maxlength && matches < count; pos = strfind(string, find, ignorecase, idx)) {
    		strdel(string, pos, pos + flen);
    		if(rlen) strins(string, replace, pos, maxlength);	// Crashy
    		idx = pos + rlen;
    		matches++;
    	}
    	return matches;
    }
     
    /**
     * Replace a character in a string
     * @param	string		The source string
     * @param	oldchar		The character to be replaced
     * @param	newchar		The character to replace with
     * @return	The number of characters replaced
     */
    stock strreplacechar(string[], oldchar, newchar)
    {
    	new matches;
    	if(ispacked(string)) {
    		if(newchar == '\0') {
    			for(new i; string{i} != '\0'; i++) {
    				if(string{i} == oldchar) {
    					strdel(string, i, i + 1);
    					matches++;
    				}
    			}
    		} else {
    			for(new i; string{i} != '\0'; i++) {
    				if(string{i} == oldchar) {
    					string{i} = newchar;
    					matches++;
    				}
    			}
    		}
    	} else {
    		if(newchar == '\0') {
    			for(new i; string[i] != '\0'; i++) {
    				if(string[i] == oldchar) {
    					strdel(string, i, i + 1);
    					matches++;
    				}
    			}
    		} else {
    			for(new i; string[i] != '\0'; i++) {
    				if(string[i] == oldchar) {
    					string[i] = newchar;
    					matches++;
    				}
    			}
    		}
    	}
    	return matches;
    }
    A gaming community ran by one person, with servers made by individuals (in this case, me, myself)
    I plan to look for a trusted person who will help me create a decent roleplay/deathmatch server on Vault-TEC,
    PM me if you're interested

Similar Threads

  1. Useful Function Thread (Frequently updated)
    By Lorenc in forum Releases
    Replies: 22
    Last Post: 10-06-2011, 05:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •