仅选择字符串 C++ 中的前几个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34097048/
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
Selecting only the first few characters in a string C++
提问by user3483203
I want to select the first 8 characters of a string using C++. Right now I create a temporary string which is 8 characters long, and fill it with the first 8 characters of another string.
我想使用 C++ 选择字符串的前 8 个字符。现在我创建了一个 8 个字符长的临时字符串,并用另一个字符串的前 8 个字符填充它。
However, if the other string is not 8 characters long, I am left with unwanted whitespace.
但是,如果另一个字符串的长度不是 8 个字符,则会留下不需要的空格。
string message = " ";
const char * word = holder.c_str();
for(int i = 0; i<message.length(); i++)
message[i] = word[i];
If word
is "123456789abc"
, this code works correctly and message
contains "12345678"
.
如果word
是"123456789abc"
,则此代码可以正常工作并message
包含"12345678"
.
However, if word
is shorter, something like "1234"
, message ends up being "1234 "
但是,如果word
较短,例如"1234"
, message 最终会成为"1234 "
How can I select either the first eight characters of a string, or the entire string if it is shorter than 8 characters?
如果字符串少于 8 个字符,如何选择字符串的前八个字符或整个字符串?
回答by cadaniluk
Just use std::string::substr
:
只需使用std::string::substr
:
std::string str = "123456789abc";
std::string first_eight = str.substr(0, 8);
回答by Vlad from Moscow
If I have understood correctly you then just write
如果我理解正确,你就写
std::string message = holder.substr( 0, 8 );
Jf you need to grab characters from a character array then you can write for example
如果您需要从字符数组中获取字符,那么您可以编写例如
const char *s = "Some string";
std::string message( s, std::min<size_t>( 8, std::strlen( s ) );
回答by Katie Stevers
Or you could use this:
或者你可以使用这个:
#include <climits>
cin.ignore(numeric_limits<streamsize>::max(), '\n');
If the max is 8 it'll stop there. But you would have to set
如果最大值是 8,它就会停在那里。但是你必须设置
const char * word = holder.c_str();
to 8. I believe that you could do that by writing
8. 我相信你可以通过写作做到这一点
const int SIZE = 9;
char * word = holder.c_str();
Let me know if this works.
让我知道这个是否奏效。
If they hit space at any point it would only read up to the space.
如果他们在任何时候击中空间,它只会读取到空间。
回答by Will Custode
char* messageBefore = "12345678asdfg"
int length = strlen(messageBefore);
char* messageAfter = new char[length];
for(int index = 0; index < length; index++)
{
char beforeLetter = messageBefore[index];
// 48 is the char code for 0 and
if(beforeLetter >= 48 && beforeLetter <= 57)
{
messageAfter[index] = beforeLetter;
}
else
{
messageAfter[index] = ' ';
}
}
This will create a character array of the proper size and transfer over every numeric character (0-9) and replace non-numerics with spaces. This sounds like what you're looking for.
这将创建一个适当大小的字符数组,并在每个数字字符 (0-9) 上进行传输,并用空格替换非数字字符。这听起来像你要找的。
Given what other people have interpreted based on your question, you can easily modify the above approach to give you a resulting string that only contains the numeric portion.
鉴于其他人根据您的问题做出的解释,您可以轻松修改上述方法,为您提供仅包含数字部分的结果字符串。
Something like:
就像是:
int length = strlen(messageBefore);
int numericLength = 0;
while(numericLength < length &&
messageBefore[numericLength] >= 48 &&
messageBefore[numericLength] <= 57)
{
numericLength++;
}
Then use numericLength
in the previous logic in place of length
and you'll get the first bunch of numeric characters.
然后numericLength
在前面的逻辑中使用代替,length
您将获得第一组数字字符。
Hope this helps!
希望这可以帮助!