紫影龙的编程日记 —— 字符串处理技术

今天是我学习编程开发的第五天,今天我主要学习的内容是字符串处理技术。
首先,我们先来看看什么是字符串~

字符串或串(String)是由数字、字母、下划线组成的一串字符。它是编程语言中表示文本的数据类型。
在程序设计中,字符串(string)为符号或数值的一个连续序列,如符号串(一串字符)或二进制数字串(一串二进制数字)。
通常以串的整体作为操作对象,如:在串中查找某个子串、求取一个子串、在串的某个位置上插入一个子串以及删除一个子串等。两个字符串相等的充要条件是:长度相等,并且各个对应位置上的字符都相等。设p、q是两个串,求q在p中首次出现的位置的运算叫做模式匹配。串的两种最基本的存储方式是顺序存储方式和链接存储方式。
了解了字符串是什么之后,我们来看看对于字符串有些什么样的处理技术

指定字符分割字符串
CString Sample(CString m_Src, CString m_Tar) //传入源字符串和目标字符串
{
    CString s1,s2,tmp;                       //定义3个 CString 类型变量
    int pos = m_Src.Find(m_Tar);             //Find返回以零开始的序号
    while(pos > 0)
    {
        s1 = m_Src.Left(pos);                //分割字符串前面的字符串
        s2 = m_Src.Right(m_Src.GetLength() - pos - 1); //分割字符串后面的字符串
        tmp += s1 + "\r\n";                  //临时字符串变量,用于储存分割字符串前面的字符串

        pos = m_Src.Find(m_Tar);             //继续查找目标字符串
    }

    return tmp;                              //返回所有分割字符串前面的字符串
}

获取字符串中的全部汉字

CString Sample(CString Text)
{
    CString tmp;
    for(int i = 0; i < Text.GetLength(); i++)
    {
        char ch = Text.GetAt(i);
        if(IsDBCSLeadByte(ch))  //判断是不是汉字
        {
            tmp += Text.Mid(i,2);
            i++;
        }
    }

    return tmp;
}

获取字符串中的所有数字

CString Sample(CString Text)
{
    CString tmp, Result;
    BOOL bcon = TRUE;      //是否找到数字

    for(int i=0; i<strtxt.GetLength(); i++) //for循环
    {
        char ch = Text.GetAt(i);
        if(IsDBCSLeadByte(ch))  //是否是中文汉字
            i++;

        if(ch >= '0' && ch <= '9') //若大于等于0并且小于等于9
        {
            bcon = TRUE;   //找到数字啦
            if(bcon)       //如果找到数字了
            {
                tmp.Format("%c",ch); //格式化字符串
                Result += tmp;
            }
        }
        else
        {
            if(!Text.IsEmpty())  //如果不为空
                bcon = FALSE;    //没有找到数字
            else
                Result = "不能输入空的字符串";
        }
        if(!bcon)
            Result = "没有找到任何数字";
    }

    return Result;
}

获得字符串中的全部英文字符

CString Sample(Text)
{
    CString Result, tmp;
    BOOL bcon = TRUE;

    for(int i = 0; i < Text.GetLength(); i++)
    {
        char ch = Text.GetAt(i);
        if(IsDBCSLeadByte(ch))
        {
            bcon = FALSE;
            i++;
        }
        else
        {
            bcon = TRUE;
            if(bcon)
            {
                tmp.Format("%c", ch);
                Result += tmp;
            }
        }
        if(!bcon)
            Result = "没有找到英文字符";
    }

    return Result;
}

字符串中的英文字符全部变为小写

CString Sample(CString Text)
{
    return Text.MakeLower();
}

字符串中的英文字符全部变为大写

CString Sample(CString Text)
{
    return Text.MakeUpper();
}

将选定英文字符变为大写

CString Sample(CString Text, int istart, int iend)
{
    CString s1, s2, Result, tmp;

    if(istart == iend)   //没有进行选择
        return "没有进行选择";    

    if(iend - istart == 2)
    {
        return "选择的是汉字或是多个字符";
    }

    char ch = strtxt.GetAt(istart);
    if(ch >= 'A' && ch <= 'Z')    //判断是否为大写
    {
        return "所选字符已是大写";
    }

    if(ch >= 'a' && ch <= 'z')    //判断是否为小写
    {
        tmp.Format("%c",ch - 32); //转换为大写  若要大写转小写则 +32
    }
    else                          //其他字符
    {
        return "不可以转换字符";
    }

    s1 = Text.Left(istart);                   //取得所选字符左边字符串
    s2 = Text.Right(Text.GetLength() - iend); //取得所选字符右边字符串

    Result = s1;
    Result += tmp;
    Result += s2;

    return Result;
}

向源字符串中的指定位置添加目标字符串

CString Sample(CString Text, CString Target, int pos)  //pos 指定位置序号
{
    CString tmp;

    tmp  = Text.Left(pos);
    Text = Text.Right(Text.GetLength() - pos);
    tmp += Target;
    tmp += Text;

    return tmp;
}

在目标字符串中删除指定位置的字符串

CString Sample(CString Text, int istart, int iend) 
{
    CString s1, s2, tmp;

    //istart 是所选字符在整个字符串中的位置序号,序号从零开始
    //iend 是所选字符的下一个字符的位置序号,序号从零开始

    if(istart == iend)
        return;

    s1 = Text.Left(istart);      //取左字符从一开始
    if(iend >= Text.GetLength())
        s2 = "";
    else
        s2 = Text.Right(Text.GetLength() - iend);   

    tmp += s1;
    tmp += s2;

    return tmp;
}

在源字符串中替换目标字符串

CString Sample(CString Text, CString Source, CString Target)
{
    Text.Replace(Source, Target);
    return Text;
}

判断源字符串里是否含有目标字符串

BOOL Sample(CString Source, CString Target)
{
    int pos = Source.Find(Target);
    return pos > 0 ? TRUE : FALSE;  //如果找到了(pos > 0) 返回真(TRUE) 否则返回假(FALSE)
}

源字符串与目标字符串的比较

BOOL Sample(CString Source, CString Target)
{
    BOOL IsFind = Source.Compare(Target));
    return IsFind > 0 ? TRUE : FALSE;  //如果相同(IsFind > 0) 返回真(TRUE) 否则返回假(FALSE)
}

源字符串与目标字符串的比较 (忽略大小写)

BOOL Sample(CString Source, CString Target)
{
    BOOL IsFind = Source.CompareNoCase(Target));
    return IsFind > 0 ? TRUE : FALSE;  //如果相同(IsFind > 0) 返回真(TRUE) 否则返回假(FALSE)
}

给字符串加密

CString Sample(CString Text, CString Password)
{
    BYTE *pwd;
    pwd = new BYTE[Password.GetLength()];
    pwd = (BYTE*)Password.GetBuffer(0);
    for(int i=1; i<Password.GetLength(); i++)
    {
        pwd[i] = pwd[i] & pwd[i-1];
    }

    BYTE *ptxt;
    ptxt = new BYTE[Text.GetLength()];
    ptxt = (BYTE*)Text.GetBuffer(0);
    for(int j=0; j<Text.GetLength(); j++)
    {
        ptxt[j] = ptxt[j] ^ pwd[strpwd.GetLength() - 1];
    }

    Text.Format("%s", ptxt);
    return Text;
}

为源字符串中选择的目标字符串添加双引号

CString Sample(CString Source, CString Target, int istart, int iend)
{
    CString s1, s2, Result, tmp;

    if(istart == iend)
    {
        return "没有进行选择";
    }

    //没有对汉字进行处理
    tmp = Source.Mid(istart, iend - istart);        //获取选中的字符
    s1  = Source.Left(istart);                      //取得所选字符左边字符串
    s2  = Source.Right(strText.GetLength() - iend); //取得所选字符右边字符串

    Result = s1;
    Result += strLChar; //左双引号
    Result += tmp;
    Result += strRChar; //右双引号
    Result += s2;

    return Result;
}

翻转字符串

CString Sample(CString Text)
{
    return Text.MakeReverse();
}

去掉首尾多余的空格

CString Sample(CString Text)
{
    char *pBuffer,*pLTmp,*pRTmp;

    pLTmp = new char[strText.GetLength()+1];
    pRTmp = new char[strText.GetLength()+1];
    pBuffer = strText.GetBuffer(0);
    StringLTrim(pBuffer,pLTmp);
    StringRTrim(pLTmp,pRTmp);
}

复制选中内容到剪切板

void Sample(CString Text, int istart, int iend)
{
    if(istart == iend)
        return ; //没有进行选择;

    CString tmp = Text.Mid(istart, iend - istart);

    ::OpenClipboard(this->GetSafeHwnd());
    EmptyClipboard();
    HGLOBAL hGlobal=GlobalAlloc(GMEM_FIXED,tmp.GetLength() + 1);
    HANDLE hmem = GlobalLock(hGlobal);
    memcpy(hmem,Text.GetBuffer(0),tmp.GetLength() + 1);
    GlobalUnlock(hGlobal);
    SetClipboardData(CF_TEXT,hGlobal);
    CloseClipboard();
}

获取源字符串中目标字符串的出现位置

CString Sample(CString Source, CString Target)
{
    CString tmp, Result;

    int pos = Source.Find(Target);
    int leftpos = 0;
    if(pos < 0)
    {
        return "没有找到指定字符";
    }

    Result = "出现位置是:";
    if(pos == 0) //要查找的字符出现在第一个位置
    {
        tmp.Format("%d",pos);
        Result += tmp;

        //如果是中文, pos 本身是两个字节
        if(IsDBCSLeadByte(Source.GetAt(pos)))
        {
            Source   = Source.Right(Source.GetLength() - pos - 2);
            leftpos += (pos+2);
        }
        else
        {
            Source   = Source.Right(Source.GetLength() - pos - 1);
            leftpos += (pos+1);
        }

        pos = Source.Find(strchar);
    }
    while(pos > 0)
    {
        tmp.Format("%d",pos + leftpos);
        if(leftpos != 0)//第一个值前不应该有分割符
        Result += " ";
        Result += tmp;

        if(IsDBCSLeadByte(strtxt.GetAt(pos)))
        {
            Source   = Source.Right(Source.GetLength() - pos - 2);
            leftpos += (pos+2);
        }
        else
        {
            Source   = Source.Right(Source.GetLength() - pos - 1);
            leftpos += (pos+1);
        }

        pos = Source.Find(Target);
    }

    return Result;
}

哎呀。。网络又出故障啦。。。
下次继续。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值