简体   繁体   English

如何使用 C++ 从字符串中删除前导零?

[英]How to remove leading zeros from string using C++?

I want to remove leading zeroes from string like "0000000057" .我想从像"0000000057"这样的字符串中删除前导零。

I did like this but didn't get any result:我确实喜欢这个,但没有得到任何结果:

string AccProcPeriNum = strCustData.substr(pos, 13);

string p48Z03 = AccProcPeriNum.substr(3, 10);

I want output only 57 .我只想要输出57

Any idea in C++?在 C++ 中有什么想法吗?

#include <string>    

std::string str = "0000000057";
str.erase(0, str.find_first_not_of('0'));

assert(str == "57");

LIVE DEMO现场演示

Piotr S's answer is good but there is one case it will return the wrong answer, that is the all-zero case: Piotr S 的回答很好,但有一种情况会返回错误的答案,即全零情况:

000000000000

To consider this in, use:要考虑这一点,请使用:

str.erase(0, min(str.find_first_not_of('0'), str.size()-1));

Even when str.size() will be 0 , it will also work.即使当str.size()0 ,它也会起作用。

Although it's probably not the most efficient (in terms of run-time speed) way to do things, I'd be tempted to just do something like:尽管这可能不是最有效的(就运行时速度而言)做事的方式,但我很想这样做:

std::cout << std::stoi(strCustData);

This is simple and straightforward to use, and gives a reasonable output (a single '0') when the input consists entirely of 0 s.这使用起来简单直接,并且在输入完全由0组成时给出了合理的输出(单个“0”)。 Only when/if profiling showed that this simple solution was a problem would I consider writing substantially more complex code in the hopes of improving speed (and I doubt that would arise).只有当/如果分析表明这个简单的解决方案是一个问题,我才会考虑编写更复杂的代码以希望提高速度(我怀疑会出现这种情况)。

The obvious limitation here is that this does assume that the characters after the leading zeros are digits.这里明显的限制是,这确实假设前导零后面的字符是数字。 That's clearly true in your sample, but I suppose it's conceivable that you have data where it's not true.这在您的样本中显然是正确的,但我想可以想象您拥有不正确的数据。

This should work as a generic function that can be applied to any of the std::basic_string types (including std::string ):这应该作为可以应用于任何std::basic_string类型(包括std::string )的通用函数:

template <typename T_STR, typename T_CHAR>
T_STR remove_leading(T_STR const & str, T_CHAR c)
{
    auto end = str.end();

    for (auto i = str.begin(); i != end; ++i) {
        if (*i != c) {
            return T_STR(i, end);
        }
    }

    // All characters were leading or the string is empty.
    return T_STR();
}

In your case you would use it like this:在你的情况下,你会像这样使用它:

string x = "0000000057";
string trimmed = remove_leading(x, '0');

// trimmed is now "57"

( See a demo .) 见演示。)

#include<algorithm>
#include<iostream>
#include<string>
using namespace std;


int main()
{
   string s="00000001";
   cout<<s<<endl;
   int a=stoi(s);
   cout<<a;
   //1 is ur ans zeros are removed


}

This C language friendly code removes leading zeros and can be used as long as the char array's length fits within an int :这个 C 语言友好代码删除前导零,只要 char 数组的长度适合int就可以使用:

char charArray[6] = "000342";
int inputLen = 6;

void stripLeadingZeros() {
    int firstNonZeroDigitAt=0, inputLen = strlen(charArray);

    //find location of first non-zero digit, if any
    while(charArray[firstNonZeroDigitAt] == '0')
        firstNonZeroDigitAt++;

    //string has no leading zeros
    if(firstNonZeroDigitAt==0)
        return; 

    // string is all zeros
    if(firstNonZeroDigitAt==inputLen) {
        memset(charArray, 0, sizeof charArray);
        strcpy(charArray, "0");
        inputLen = 1;
        return;
    }

    //shift the non-zero characters down
    for(int j=0; j<inputLen-firstNonZeroDigitAt; j++) {
        charArray[j] = charArray[firstNonZeroDigitAt+j];
    }

    //clear the occupied area and update string length
    memset(charArray+inputLen-firstNonZeroDigitAt, 0, inputLen-firstNonZeroDigitAt+1);
    inputLen -= firstNonZeroDigitAt;
}

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

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