Aeronix
09-03-2011, 04:10 PM
/*
* 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.
stock replacechars(string[], match, replace)
{
for(new i=0; string[i]; i++)
{
if(string[i] == match)
{
string[i] = replace;
}
}
}
Enjoy.
Lorenc
09-03-2011, 06:35 PM
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:
#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;
}
Powered by vBulletin® Version 4.2.1 Copyright © 2023 vBulletin Solutions, Inc. All rights reserved.