字符串去除分隔符

//算法1
 
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool isValidHexadecimalChar(const char ascii_data)
{
    bool result = false;

    result = (ascii_data >= '0' && ascii_data <= '9') || (ascii_data >= 'A' && ascii_data <= 'F')
            || (ascii_data >= 'a' && ascii_data <= 'f');

    cout << int(result) << endl;

    return result;
}

void stringRemoveSeparators(char *ascii_data)
{
    size_t fPreIndex = 0;
    size_t fSuffIndex = 0;

    while(*(ascii_data + fPreIndex))
    {
		cout << ascii_data + fPreIndex << endl;

        if(isValidHexadecimalChar((const char)(*(ascii_data + fPreIndex))))
        {
            *(ascii_data + fSuffIndex) = *(ascii_data + fPreIndex);
            fSuffIndex++;
        }

        fPreIndex++;
    }
	
	while(fSuffIndex < fPreIndex)
	{
		*(ascii_data + fSuffIndex) = 0;
		fSuffIndex++;
	}
}

int main(int argc, char** argv)
{
    char pr[] = "_0000_00_00"; //不能这样写:char* pr = "_0000_00_00";
	cout << pr << endl;

    stringRemoveSeparators(pr);

    cout << pr << endl;
    return 0;
}

注意:

char* pr = "_0000_00_00"; 是一个字符串常量,存储在常量区,常量区存储的东西是不能够被修改的。

使用string来实现: 

   std::string strConvertor(asciiData);
   strConvertor.erase(remove_if(strConvertor.begin(), strConvertor.end(), [](char x) { return x == '_'; }), strConvertor.end());

猜你喜欢

转载自blog.csdn.net/xikangsoon/article/details/114583900