简体   繁体   English

在C中分割多行字符串

[英]split a multi-line string in C

how can I split a multi-line string in const char* array.? 如何在const char *数组中拆分多行字符串?

Here is the input string. 这是输入字符串。

const char* str = "122.123,-38.789"
                  "122.123,-39.78";

I need to get two string from this. 我需要从中得到两个字符串。

str1 = "122.123,-38.789";

str2 = "122.123,-39.78";

output of 输出

printf("string='%s'", str)

string='122.123,-38.789122.123,-39.78'

What should be done to split this string ? 应该怎么做才能拆分此字符串?

Use an array of char * 使用char数组*

#include <stdio.h>
const char* str[] = { "122.123,-38.789" ,  "122.123,-39.78" };

int main(){
  printf("%s\n%s\n",str[0],str[1] );
  return 0;
}

How the compiler understood your code: 编译器如何理解您的代码:

The C pre-processor concats strings placed together, C预处理器将字符串放置在一起,

const char* str = "122.123,-38.789"
                  "122.123,-39.78";

The linebreak between the strings is parsed as a blank, treated the same as a space or a tab. 字符串之间的换行符被解析为空白,与空格或制表符相同。 So it's equivalent to: 因此,它等效于:

const char* str = "122.123,-38.789" "122.123,-39.78";

which the preprocessor converts to 预处理器转换为

const char* str = "122.123,-38.789122.123,-39.78";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM