简体   繁体   English

如何仅删除非数字字符串的第一个字符? (MFC,C ++)

[英]How do I remove only the first character of a string that is not a digit? (MFC, C++)

I want to remove only the first character in a string that is NOT a digit. 我只想删除不是数字的字符串中的第一个字符。 The first character can be anything from 'A' to 'Z' or it may be a special character like '&' or '#'. 第一个字符可以是从“ A”到“ Z”的任何字符,也可以是特殊字符,例如“&”或“#”。 This legacy code is written in MFC. 这些旧代码是用MFC编写的。 I've looked at the CString class but cannot figure out how to make this work. 我已经看过CString类,但是无法弄清楚如何使这项工作。

I have strings that may look like any of the following: J22008943452GF or 22008943452GF or K33423333333IF or 23000526987IF or #12000895236GF. 我有看起来像以下任何字符串的字符串:J22008943452GF或22008943452GF或K33423333333IF或23000526987IF或#12000895236GF。 You get the idea by now. 您现在知道了。

My dilemma is I need to remove the character in the first position of all the strings, but not the strings that starts with a digit. 我的困境是我需要删除所有字符串的第一个位置的字符,而不是以数字开头的字符串。 For the strings that begin with a digit, I need to leave them alone. 对于以数字开头的字符串,我需要不理会它们。 Also, none of the other characters in the string should not be altered. 同样,不应更改字符串中的其他任何字符。 For example the 'G', 'I' or 'F' in the later part of the string should not be changed. 例如,不应更改字符串后半部分的“ G”,“ I”或“ F”。 The length of the string will always be 13 or 14 digits. 字符串的长度将始终为13或14位数字。

Here is what I have so far. 这是我到目前为止所拥有的。

CString GAbsMeterCalibration::TrimMeterSNString (CString meterSN)
 {
   meterSN.MakeUpper();
   CString TrimmedMeterSNString = meterSN;
   int strlength = strlen(TrimmedMeterSNString);

    if (strlength == 13)
       {
         // Check the first character anyway, even though it’s
         //  probably okay.  If it is a digit, life’s good.
         //  Return unaltered TrimmedMeterSNString;
       }

    if (strlength == 14)) 
       {
           //Check the first character, it’s probably going
           //  to be wrong and is a character, not a digit.  

           // if I find a char in the first postion of the
           //    string, delete it and shift everything to the 
           //    left.  Make this my new TrimmedMeterSNString
           //  return altered TrimmedMeterSNString;
        }
 }

The string lengths are checked and validated before the calls. 在调用之前检查并验证字符串长度。

From my investigations, I've found that MFC does not have a regular expression class. 从我的调查中,我发现MFC没有正则表达式类。 Nor does it have the substring methods. 它也没有substring方法。

From what I understand, you want to remove the first letter if it is not a digit. 据我了解,如果不是数字,您想删除第一个字母。 So you may make this function simpler: 因此,您可以使此函数更简单:

CString GAbsMeterCalibration::TrimMeterSNString(CString meterSN)
{
    meterSN.MakeUpper();

    int length = meterSN.GetLength();

    // just check the first character is always a digit else remove it
    if (length > 0 && unsigned(meterSN[0] - TCHAR('0')) > unsigned('9'))
    {
        return meterSN.Right(length - 1);
    }

    return meterSN;
}

I am not using function isdigit instead of the conditional trick with unsigned because CString uses TCHAR which can be either char or wchar_t . 我没有使用函数isdigit而不是带有unsigned的条件技巧,因为CString使用的TCHAR可以是charwchar_t

The solution is fairly straight forward: 解决方案相当简单:

CString GAbsMeterCalibration::TrimMeterSNString(CString meterSN) {
    meterSN.MakeUpper();
    return _istdigit(meterSN.GetAt(0)) ? meterSN :
                                         meterSN.Mid(1);
}

The implementation can be compiled for both ANSI and Unicode project settings by using _istdigit . 可以使用_istdigit为ANSI和Unicode项目设置编译实现。 This is required since you are using CString , which stores either MBCS or Unicode character strings. 这是必需的,因为您使用的是CString ,它存储MBCS或Unicode字符串。 The desired substring is extracted using CStringT::Mid . 使用CStringT::Mid提取所需的子字符串。

(Note that CString is a typedef for a specific CStringT template instantiation, depending on your project settings.) (请注意, CString是特定CStringT模板实例化的typedef,具体取决于您的项目设置。)

How about: 怎么样:

CString GAbsMeterCalibration::TrimMeterSNString (CString meterSN)
 {
   meterSN.MakeUpper();
   CString TrimmedMeterSNString = meterSN;
   int strlength = strlen(TrimmedMeterSNString);

    if (std::isdigit(TrimmedMeterSNString.GetAt(0)) )
       {
         // Check the first character anyway, even though it’s
         //  probably okay.  If it is a digit, life’s good.
         //  Return unaltered TrimmedMeterSNString;
       }
 }
CString test="12355adaddfca";
if((test.GetAt(0)>=48)&&(test.GetAt(0)<=57))
{
//48 and 57 are ascii values of 0&9, hence this is a digit
//do your stuff
 //CString::GetBuffer may help here??
}
else
{
//it is not a digit, do your stuff

}

Compare the ascii value of the first position in the string and you know if it's a digit or not.. 比较字符串中第一个位置的ascii值,您会知道它是否是数字。

I don't know if you've tried this, but, it should work. 我不知道您是否尝试过此方法,但是应该可以。

CString str = _T("#12000895236GF");
//  check string to see if it starts with digit.
CString result = str.SpanIncluding(_T("0123456789"));
//  if result is empty, string does not start with a number
//  and we can remove the first character.  Otherwise, string
//  remains intact.
if (result.IsEmpty())
    str = str.Mid(1);

Seems a little easier than what's been proposed. 似乎比建议的要容易一些。

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

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