Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字符串到Arduino中的const char*?_C++_C_String_Char_Arduino - Fatal编程技术网

C++ 字符串到Arduino中的const char*?

C++ 字符串到Arduino中的const char*?,c++,c,string,char,arduino,C++,C,String,Char,Arduino,我有一个变量tweet,它是一个字符串,它的开头有一个我想剪掉的字符 所以我要做的是使用strstr()删除它。这是我的密码: tweet = strstr(tweet, "]"); 但是,我得到了这个错误: cannot convert 'String' to 'const char*' for argument '1' to 'char' strstr(const char*, const char*) 所以我的想法是将tweet转换成一个字符。我该怎么做呢?使用下面的语句tweet.

我有一个变量
tweet
,它是一个字符串,它的开头有一个我想剪掉的字符

所以我要做的是使用
strstr()
删除它。这是我的密码:

tweet = strstr(tweet, "]");
但是,我得到了这个错误:

cannot convert 'String' to 'const char*' for argument '1' to 
'char' strstr(const char*, const char*)

所以我的想法是将
tweet
转换成一个字符。我该怎么做呢?

使用下面的语句
tweet.c_str()
将返回字符串缓冲区,这将允许您执行所需的编辑。

string
有一个
c_str()
成员函数,该函数返回
const char*

您可以更轻松地完成这项工作。因为您使用的是C++:

tweet = tweet.substring(1);
substr()将字符串的一部分作为字符串返回给您。 该参数是该子字符串的起点。 因为字符串索引是基于0的,所以1应该剪掉第一个字符

如果您想使用strstr,您可以将tweet转换为c字符串:

tweet = strstr( tweet.c_str(), "]" );

然而,这是非常低效的,因为它返回一个c字符串,必须将其转换为std::string,以适应tweet。这将比在不同类型的字符串之间转换更容易混淆

看看:

string.indexOf(val)
string.indexOf(val, from)
参数

string: a variable of type String
val: the value to search for - char or String
from: the index to start the search from

我意识到这是一个老问题,但是如果你想,比方说,比较一个特定的字符,而不是一个字符串中的一个字母,那么你想要的是string.charAt(n)。例如,如果您正在进行串行编程,并且需要检查STX(\02),则可以使用以下代码

char STX = '\02'

if (inputString.charAt(0) == STX) {
  doSomething();
}

使用
c_str()
方法获得一个
const char*
看看
String
的定义,看看是否有合适的函数(比如
c_str()
std::String的成员函数)——希望有一些方法可以做你想做的事情,而不会弄乱c风格的字符串。在不知道字符串是什么的情况下,这个问题无法回答。@Andrew为什么要删除arduino标记?这就从问题中删除了重要信息!编写多语言源文件是一项艰巨的工作。我建议你坚持C或C++ +1来识别ARDUIO字符串类,而不是C++ STD::字符串