[转]vc 汉字汉语拼音首字母如何获取

//源码:在vc中实现获取汉字拼音的首字母

Author: lixiaosan
Date: 05/26/2006
/********************************************************************************************
/* Syntax
/*        GetFirstLetter(CString strName, CString& strFirstLetter)
/* Remarks:
/*        Get the first letter of pinyin according to specified Chinese character.
/* Parameters:
/*        strName            - a CString object that is to be parsed.
/*        strFirstLetter     - a CString object that is to receive the string of the first letter.
/* Return Values:
/*        None.
/* Author:
/*        lixiaosan
/* Create Date:
/*        05-26-2006
/********************************************************************************************
void GetFirstLetter(CString strName, CString& strFirstLetter)
{
    TBYTE ucHigh, ucLow;
    int nCode;
    CString strRet;

    strFirstLetter.Empty();

    for (int i=0; i<strName.GetLength(); i++)
    {
        if ( (TBYTE)strName[i] < 0x80 )
            continue;

        ucHigh = (TBYTE)strName[i];
        ucLow = (TBYTE)strName[i+1];
        if ( ucHigh < 0xa1 || ucLow < 0xa1)
            continue;
        else
            // Treat code by section-position as an int type parameter,
            // so make following change to nCode.
            nCode = (ucHigh - 0xa0) * 100 + ucLow - 0xa0;

        FirstLetter(nCode, strRet);
        strFirstLetter += strRet;
        i++;
    }
}

/********************************************************************************************
/* Syntax
/*        void FirstLetter(int nCode, CString& strLetter)
/* Remarks:
/*        Get the first letter of pinyin according to specified Chinese character code.
/* Parameters:
/*        nCode         - the code of the chinese character.
/*        strLetter      - a CString object that is to receive the string of the first letter.
/* Return Values:
/*        None.
/* Author:
/*        lixiaosan
/* Create Date:
/*        05-26-2006
/********************************************************************************************
void FirstLetter(int nCode, CString& strLetter)
{
   if(nCode >= 1601 && nCode < 1637) strLetter = _T("A");
   if(nCode >= 1637 && nCode < 1833) strLetter = _T("B");
   if(nCode >= 1833 && nCode < 2078) strLetter = _T("C");
   if(nCode >= 2078 && nCode < 2274) strLetter = _T("D");
   if(nCode >= 2274 && nCode < 2302) strLetter = _T("E");
   if(nCode >= 2302 && nCode < 2433) strLetter = _T("F");
   if(nCode >= 2433 && nCode < 2594) strLetter = _T("G");
   if(nCode >= 2594 && nCode < 2787) strLetter = _T("H");
   if(nCode >= 2787 && nCode < 3106) strLetter = _T("J");
   if(nCode >= 3106 && nCode < 3212) strLetter = _T("K");
   if(nCode >= 3212 && nCode < 3472) strLetter = _T("L");
   if(nCode >= 3472 && nCode < 3635) strLetter = _T("M");
   if(nCode >= 3635 && nCode < 3722) strLetter = _T("N");
   if(nCode >= 3722 && nCode < 3730) strLetter = _T("O");
   if(nCode >= 3730 && nCode < 3858) strLetter = _T("P");
   if(nCode >= 3858 && nCode < 4027) strLetter = _T("Q");
   if(nCode >= 4027 && nCode < 4086) strLetter = _T("R");
   if(nCode >= 4086 && nCode < 4390) strLetter = _T("S");
   if(nCode >= 4390 && nCode < 4558) strLetter = _T("T");
   if(nCode >= 4558 && nCode < 4684) strLetter = _T("W");
   if(nCode >= 4684 && nCode < 4925) strLetter = _T("X");
   if(nCode >= 4925 && nCode < 5249) strLetter = _T("Y");
   if(nCode >= 5249 && nCode < 5590) strLetter = _T("Z");
}


How to use:

    CString strName, strRes;
    strName = _T("A李小三");
    GetFirstLetter(strName, strRes);
    //Then the value of strRes is "LXS".

posted on 2012-04-06 17:27  和轩僮  阅读(730)  评论(0编辑  收藏  举报

导航