我怎么能用C++中的另一个子字符串替换字符串中的子字符串,我可以使用哪些函数?
eg: string test = "abc def abc def";
test.replace("abc", "hij").replace("def", "klm"); //replace occurrence of abc and def with other substring
Run Code Online (Sandbox Code Playgroud) 我有一个很长的NSString,我试图替换特殊字符.我的部分字符串如下所示:
"veau (c\u00f4telette)","veau (filet)","agneau (gigot)","agneau (c\u00f4telette)","b**\u0153**uf (hach\u00e9)","porc (hach\u00e9)"
Run Code Online (Sandbox Code Playgroud)
我想用"oe"替换所有\ u0153.我试过了:
[response stringByReplacingOccurrencesOfString:@"\u0153" withString:@"oe"];
Run Code Online (Sandbox Code Playgroud)
但它不起作用....我不明白为什么!
这个例子有效,但我认为内存泄漏.如果使用此功能,则在简单Web服务器模块中使用的函数会因共享内存而增加.
char *str_replace ( const char *string, const char *substr, const char *replacement ){
char *tok = NULL;
char *newstr = NULL;
char *oldstr = NULL;
if ( substr == NULL || replacement == NULL ) return strdup (string);
newstr = strdup (string);
while ( (tok = strstr ( newstr, substr ))){
oldstr = newstr;
newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 );
memset(newstr,0,strlen ( oldstr ) …Run Code Online (Sandbox Code Playgroud)