我可以拆分C字符串吗? [英] can I split a C string

查看:84
本文介绍了我可以拆分C字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

可以拆分这样的C字符串吗?

char * p =" Hello \

World \\ \\ n \\ n \\ n  \\ n \\ n 

这是根据标准吗?

任何关于上述疑问的标准的帮助或链接是

aprreciated。 ..

问候,

Kiran

解决方案

KIRAN写道:
< blockquote class =post_quotes>
大家好,

我可以像这样分割一个C字符串吗?

char * p =" Hello \

World \\\\ r" ;;

这是根据标准吗?

关于上述疑问的标准的任何帮助或链接是

aprreciated ...

问候,

Kiran



你可以使用反斜杠拆分C程序中的任何一行。

1请注意,反斜杠和下一行换行符之间没有隐形空格

字符。

2传统方式拆分字符串文字只是简单地用引号括起部分:


char * p =" Hello"

" ; World \ n \\ ndr ;;


反斜杠主要用于冗长的宏。


-

pete


KIRAN写道:


大家好,

我可以像这样分割一个C字符串吗?

char * p =" Hello \

World \ n \ r \\ n";

这是根据标准吗?

关于上述疑问的标准的任何帮助或链接是

aprreciated ...



是的,你可以这样做(假设\紧跟在结束第一行的

换行符之前)。

标准中的相关参考是5.1.1.2p2(线性拼接发生在翻译中

阶段2)和6.4.5p4(字符串文字在阶段6中被识别)。


在Bad Old Days中,这通常是在不使用非常长的源代码行的情况下编写

非常长的字符串文字的唯一方法。

但是1989 ANSI标准编写了一个更好的解决方案:字符串

文字在源中相邻,只有白色的空间和它们之间的评论,它们被连接在一起( 5.1.1.2p6)。

所以你可以写一下


char * p =" Hello"

" World \\\\";


甚至


char * p =" Hell" / *对不起,坏话* /

" o" / *啊,那就更好* /

/ *留出额外的空格,以防万一:* /"

/ *现在,我们问候的收件人:* /

" W" / * Gimme a wuh! * /

o / *给我一个哦! * /

" r" / *给我一个啊! * /

" l" / * Gimme an ell! * /

" d" / * Gimme a dee! * /

" \ n" / *行尾* /" \r" / *只是为了好运* /

;


具有相同的效果。


-
Er*********@sun.com


KIRAN写道:


我可以拆分这样的C字符串吗?

char * p =" Hello \

World \\\\ rd ;;

这是根据标准吗?

任何有关上述标准的帮助或链接怀疑是

aprreciated ...



我认为你可以做到这一点。

但你会在Hello和World之间有3个空格。


如果你这样做可能会更好:


char * p = 你好

World\\ n;


Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Any help or link to standard regarding the above doubt is
aprreciated...
Regards,
Kiran

解决方案

KIRAN wrote:

Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Any help or link to standard regarding the above doubt is
aprreciated...
Regards,
Kiran

You can use a backslash to split any line in a C program.
1 Be careful that there are no invisible white space
characters between the backslash and the following newline.
2 The conventional way of splitting string literals is simply
to enclose the parts in quotes:

char * p = "Hello "
"World\n\r";

The backslash is mostly just used for lengthy macros.

--
pete


KIRAN wrote:

Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Any help or link to standard regarding the above doubt is
aprreciated...

Yes, you can do it (assuming the \ immediately precedes the
newline ending the first line). The relevant references in the
Standard are 5.1.1.2p2 (line splicing occurs in translation
phase 2) and 6.4.5p4 (string literals are recognized in phase 6).

In the Bad Old Days this was often the only way to write
very long string literals without using very long source lines.
But the 1989 ANSI Standard codified a better solution: String
literals that are adjacent in the source, with nothing but white
space and comments between them, are concatenated (5.1.1.2p6).
So you could write

char *p = "Hello "
" World\n\r";

or even

char *p = "Hell" /* sorry, bad word */
"o" /* ah, that''s better */
/* leave extra spaces, just in case: */ " "
/* and now, the recipient of our greeting: */
"W" /* Gimme a wuh! */
"o" /* Gimme an oh! */
"r" /* Gimme an ahh! */
"l" /* Gimme an ell! */
"d" /* Gimme a dee! */
"\n" /* end of line */ "\r" /* just for luck */
;

with the same effect.

--
Er*********@sun.com


KIRAN wrote:

can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Any help or link to standard regarding the above doubt is
aprreciated...

I think you can do that.
But you''ll have 3 spaces between Hello and World.

Maybe it''s better if you do it like this instead:

char * p = "Hello "
"World\r\n";


这篇关于我可以拆分C字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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