如何分割字符串在整个C / Objective-C的多行文字? [英] How to split a string literal across multiple lines in C / Objective-C?

查看:135
本文介绍了如何分割字符串在整个C / Objective-C的多行文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pretty长sqlite的查询:

I have a pretty long sqlite query:

const char *sql_query = "SELECT statuses.word_id FROM lang1_words, statuses WHERE statuses.word_id = lang1_words.word_id ORDER BY lang1_words.word ASC";

我怎样才能打破它在多条线路,使其更易于阅读?
如果我做了以下内容:

How can I break it in a number of lines to make it easier to read? If I do the following:

const char *sql_query = "SELECT word_id
                        FROM table1, table2
                        WHERE table2.word_id = table1.word_id
                        ORDER BY table1.word ASC";

我得到一个错误。

I am getting a error.

有没有写在多行查询的方式?

Is there a way to write queries in multiple lines?

推荐答案

有两种方式在多行分割字符串:

There are two ways to split strings over multiple lines:

在C所有线可以被分成使用\\多行

All lines in C can be split into multiple lines using \.

普通C:

char *my_string = "Line 1 \
                   Line 2";

目标-C:

NSString *my_string = @"Line1 \
                        Line2";

更好的方法

有一个更好的办法,只是为字符串工作。

Better approach

There's a better approach that works just for strings.

普通C:

char *my_string = "Line 1 "
                  "Line 2";

目标-C:

NSString *my_string = @"Line1 "
                       "Line2";    // the second @ is optional

第二种方法比较好,因为没有包括很多的空白。然而,对于一个SQL查询,这两个都是可能的。

The second approach is better, because there isn't a lot of whitespace included. For a SQL query however, both are possible.

请注意:用#define,你必须添加一个额外的\\来连接两个字符串:

NOTE: With a #define, you have to add an extra '\' to concatenate the two strings:

普通C:

#define kMyString "Line 1"\
                  "Line 2"

这篇关于如何分割字符串在整个C / Objective-C的多行文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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