简体   繁体   English

仅选择字符串C ++中的前几个字符

[英]Selecting only the first few characters in a string C++

I want to select the first 8 characters of a string using C++. 我想使用C ++选择字符串的前8个字符。 Right now I create a temporary string which is 8 characters long, and fill it with the first 8 characters of another string. 现在,我创建一个长度为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"消息最终"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个字符的整个字符串?

Just use std::string::substr : 只需使用std::string::substr

std::string str = "123456789abc";
std::string first_eight = str.substr(0, 8);

只需在字符串上调用resize

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 Jf您需要从字符数组中获取字符,然后可以编写例如

const char *s = "Some string";

std::string message( s, std::min<size_t>( 8, std::strlen( s ) );

Or you could use this: 或者,您可以使用以下代码:

#include <climits>
cin.ignore(numeric_limits<streamsize>::max(), '\n');

If the max is 8 it'll stop there. 如果最大值为8,它将在此处停止。 But you would have to set 但是你必须设置

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. 如果他们在任何时候撞到太空,它只会读到太空。

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. 这将创建适当大小的字符数组,并在每个数字字符(0-9)上转移,并用空格替换非数字。 This sounds like what you're looking for. 这听起来像您要找的东西。

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! 希望这可以帮助!

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

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