替换子串 [英] replace substring

查看:64
本文介绍了替换子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有

char * str1 =" d:\ temp\data\test.txt"


我想替换所有的\要成为\\,这样字符串就会有d:\\ temp \\\\\\\\\\ ;


你能帮忙吗?


问候,

Magix

Hi,

I have
char* str1 = "d:\temp\data\test.txt"

I want to replace all the "\" to be "\\", so that

the string will have "d:\\temp\\data\\test.txt"

Can you help ?

Regards,
Magix

推荐答案

5月26日下午2:06 * pm,magix< mag ... @ gmail.comwrote:
On May 26, 2:06*pm, magix <mag...@gmail.comwrote:

我想替换所有\成为\\,所以
I want to replace all the "\" to be "\\", so that



尝试

sed ss \\\\\\
YMMV


James Dow Allen

Try
sed ss\\\\s\\\\\\\\sg

YMMV

James Dow Allen


magix说:
magix said:




我有

char * str1 =" d:\ temp \ data \ test.txt"
Hi,

I have
char* str1 = "d:\temp\data\test.txt"



如果你有,那你就麻烦了,因为\d不是一个有效的逃脱

序列。 \t是,但我不认为你真的想要嵌入标签

字符串,是吗?

If you have that, you''re in trouble, because \d isn''t a valid escape
sequence. \t is, but I don''t think you really wanted embedded tabs in that
string, did you?


我想要的替换所有\要成为\\,这样字符串就会有d:\\ temp \\\\\\\\\\ ;


你能帮忙吗?
I want to replace all the "\" to be "\\", so that

the string will have "d:\\temp\\data\\test.txt"

Can you help ?



char * str1 =" d:\\ temp \\data\\test.txt" ;;

如果这不能解决您的问题,您需要澄清您的问题。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

char *str1 = "d:\\temp\\data\\test.txt";

If that doesn''t fix your problem, you need to clarify what your problem is.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


magix写道:
magix wrote:




我有

char * str1 =" d:\ temp\data\test.txt"


我想要全部替换\要成为\\,这样字符串就会有d:\\ temp \\\\\\\\\\ ;


你能帮忙吗?
Hi,

I have
char* str1 = "d:\temp\data\test.txt"

I want to replace all the "\" to be "\\", so that

the string will have "d:\\temp\\data\\test.txt"

Can you help ?



str1指向的字符串是字符串文字,你不应该尝试修改它。字符串文字通常是不可修改的,并且

任何情况下你的新字符串占用的空间比字符串字面数多,

所以即使你可以修改它,你也会超出结束它。


你需要为新字符串分配一个足够空间的数组

(包括终止零字节)然后像


/ *警告:既没有测试也没有写成防弹

(甚至是防摔)* /

无效替换( char * source,char * target)

{

for(; * source; source ++)

{

if(* source =''\\'')* target ++ = * source;

* target ++ = * source;

}

}


现在,上面的代码在看到它时只产生两个''\\'。

但几乎在任何系统上,它都是更有意义的是将''\\''替换为''/''

(甚至windows和dos都没有问题。他的),当上面的

然后变成

void replaceceslash(char * source,char * target)

{

for(; * source;来源++)

{

if(* source =''\\'')* target ++ =''/'';

else * target ++ = * source; / *注意别的! * /

}

}

The string to which str1 points is a string literal, and you should not
attempt to modify it. String literals are usually not modifiable and in
any case your new string takes more space than the string literal does,
so even if you could modify it, you would overrun the end of it.

You need to allocate an array of sufficient space for the new string
(including the terminating zero-byte) and then something like

/* warning: neither tested nor written to be bulletproof
(or even dart-resistant) */
void replaceslash(char *source, char *target)
{
for (;*source; source++)
{
if (*source = ''\\'') *target++ = *source;
*target++ = *source;
}
}

Now, the code above simply produces two ''\\'' when it sees one.
But on almost any system, it makes more sense to replace ''\\'' with ''/''
(even windows and dos have no problem with this), when the above
then becomes
void replaceslash(char *source, char *target)
{
for (;*source; source++)
{
if (*source = ''\\'') *target++ = ''/'';
else *target++ = *source; /* notice the else! */
}
}


这篇关于替换子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆