C++ 在 Arduino 中将字符串转换为 const char*?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8578756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:49:26  来源:igfitidea点击:

String to const char* in Arduino?

c++cstringchararduino

提问by iosfreak

I have a variable tweetthat is a string and it has a character at the very beginning that I want to clip off.

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

So what I want to do is use strstr()to remove it. Here's my code:

所以我想做的是用strstr()它来删除它。这是我的代码:

tweet = strstr(tweet, "]");

However, I get this error:

但是,我收到此错误:

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

So my thought would be to convert tweetinto a char. How would I go about doing so?

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

回答by David Grayson

How about you use substringinstead. This will be less confusing than converting between different types of string.

你换用怎么样substring。这将比在不同类型的字符串之间转换更容易混淆。

http://arduino.cc/en/Reference/StringSubstring

http://arduino.cc/en/Reference/StringSubstring

回答by Ernest Friedman-Hill

stringhas a c_str()member function that returns const char *.

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

回答by s3rius

you can do that easier. Since you're using C++:

你可以更轻松地做到这一点。由于您使用的是 C++:

tweet = tweet.substring(1);

substr() returns a part of the string back to you, as string. The parameter is the starting point of this sub string. Since string index is 0-based, 1 should clip off the first character.

substr() 将字符串的一部分作为字符串返回给您。参数是这个子串的起点。由于字符串索引是从 0 开始的,因此 1 应该剪掉第一个字符。

If you want to use strstr you can just cast tweet into a c-string:

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

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

However, that's pretty inefficient since it returns a c-string which has to be turned into a std::string against in order to fit into tweet.

但是,这是非常低效的,因为它返回一个 c 字符串,必须将其转换为 std::string 以适应推文。

回答by alf

Using the following statement tweet.c_str()will return the string buffer, which will allow you to perform the edit you want.

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

回答by pat1300

Look at:

看着:

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

Parameters

参数

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

See this page

看这个页面

回答by Brian Chow

I realize this is an old question, but if you're trying to, say, compare a specific char, and not just one letter in a string, then what you want is string.charAt(n). For example, if you're doing serial programming and you need to check for STX (\02) than you can use the following code.

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

char STX = ''

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