C++ 字符串操作常用函数

集合网上一些常用的字符串操作,全部验证可用


1.字符串替换(char*型)

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

//字符串替换函数
/********************************************************************
 *  Function:  my_strstr()
 *  Description: 在一个字符串中查找一个子串;
 *  Input:      ps: 源;      pd:子串
 *  Return :    0:源字符串中没有子串; 1:源字符串中有子串;
 *********************************************************************/
char * my_strstr(char * ps, char *pd) {
	char *pt = pd;
	int c = 0;
	while (*ps != '\0') {
		if (*ps == *pd) {
			while (*ps == *pd && *pd != '\0') {
				ps++;
				pd++;
				c++;
			}
		} else {
			ps++;
		}
		if (*pd == '\0') {
			return (ps - c);
		}
		c = 0;
		pd = pt;
	}
	return 0;
}

/********************************************************************
 *  Function:  memcpy()
 *  Description: 复制一个内存区域到另一个区域;
 *  Input:      src: 源;
 count: 复制字节数.
 *  Output:      dest: 复制目的地;
 *  Return :      dest;
 *********************************************************************/
void * memcpy(void * dest, const void *src, size_t count) {
	char *tmp = (char *) dest, *s = (char *) src;
	while (count--)
		*tmp++ = *s++;
	return dest;
}

/********************************************************************
 *  Function:  str_replace()
 *  Description: 在一个字符串中查找一个子串,并且把所有符合的子串用
 另一个替换字符串替换。
 *  Input:      p_source:要查找的母字符串; p_seach要查找的子字符串;
 p_repstr:替换的字符串;
 *  Output:      p_result:存放结果;
 *  Return :      返回替换成功的子串数量;
 *  Others:      p_result要足够大的空间存放结果,所以输入参数都要以\0结束;
 *********************************************************************/
int str_replace(char *p_result, char* p_source, char* p_seach, char *p_repstr) {
	int c = 0;
	int repstr_leng = 0;
	int searchstr_leng = 0;
	char *p1;
	char *presult = p_result;
	char *psource = p_source;
	char *prep = p_repstr;
	char *pseach = p_seach;
	int nLen = 0;
	repstr_leng = strlen(prep);
	searchstr_leng = strlen(pseach);
	do {
		p1 = my_strstr(psource, p_seach);
		if (p1 == 0) {
			strcpy(presult, psource);
			return c;
		}
		c++;  //匹配子串计数加1;
		printf("结果:%s\r\n", p_result);
		printf("源字符:%s\r\n", p_source);
		// 拷贝上一个替换点和下一个替换点中间的字符串
		nLen = p1 - psource;
		memcpy(presult, psource, nLen);
		// 拷贝需要替换的字符串
		memcpy(presult + nLen, p_repstr, repstr_leng);
		psource = p1 + searchstr_leng;
		presult = presult + nLen + repstr_leng;
	} while (p1);
	return c;
}
#define MAX 200
int main(void) {
	int i = 0;
	char s[MAX] = { 0 };        //存放源字串
	char s1[MAX] = { 0 };        //存放子字串
	char s2[MAX] = { 0 };        //存放替换字串
	char result_a[2000] = { 0 };        //存放替换结果;
	char *p, *ptm, *pr;
	puts("Please input the string for s:");
	scanf("%s", s);
	puts("Please input the string for s1:");
	scanf("%s", s1);
	puts("Please input the string for s2:");
	scanf("%s", s2);
	ptm = s;
	pr = result_a;
	i = str_replace(pr, ptm, s1, s2);
	printf("替换%d个子字符串;\r\n", i);
	printf("替换后结果:%s\r\n", result_a);
	system("pause");
}
2.字符串替换(CString型)

	CString str = L"AAABBBCCC";
	str.Replace(L"BBB", L"DDD");
	MessageBox(str);

3.字符串转换

wchar_t * ANSIToUnicode(const char* str)
{
	int textlen;
	wchar_t * result;
	textlen = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
	result = (wchar_t *)malloc((textlen + 1)*sizeof(wchar_t));
	memset(result, 0, (textlen + 1)*sizeof(wchar_t));
	MultiByteToWideChar(CP_ACP, 0, str, -1, (LPWSTR)result, textlen);
	return result;
}

char * UnicodeToANSI(const wchar_t* str)
{
	char* result;
	int textlen;
	textlen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
	result = (char *)malloc((textlen + 1)*sizeof(char));
	memset(result, 0, sizeof(char) * (textlen + 1));
	WideCharToMultiByte(CP_ACP, 0, str, -1, result, textlen, NULL, NULL);
	return result;
}

wchar_t * UTF8ToUnicode(const char* str)
{
	int textlen;
	wchar_t * result;
	textlen = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
	result = (wchar_t *)malloc((textlen + 1)*sizeof(wchar_t));
	memset(result, 0, (textlen + 1)*sizeof(wchar_t));
	MultiByteToWideChar(CP_UTF8, 0, str, -1, (LPWSTR)result, textlen);
	return result;
}

char * UnicodeToUTF8(const wchar_t* str)
{
	char* result;
	int textlen;
	textlen = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
	result = (char *)malloc((textlen + 1)*sizeof(char));
	memset(result, 0, sizeof(char) * (textlen + 1));
	WideCharToMultiByte(CP_UTF8, 0, str, -1, result, textlen, NULL, NULL);
	return result;
}
char* ANSIToUTF8(const char* str)
{
	return UnicodeToUTF8(ANSIToUnicode(str));
}

char* UTF8ToANSI(const char* str)
{
	return UnicodeToANSI(UTF8ToUnicode(str));
}


//UTF-8 GBK  
int UTF82GBK(char *szUtf8, char *szGbk, int Len)
{
	int n = MultiByteToWideChar(CP_UTF8, 0, szUtf8, -1, NULL, 0);
	WCHAR * wszGBK = new WCHAR[sizeof(WCHAR) * n];
	memset(wszGBK, 0, sizeof(WCHAR) * n);
	MultiByteToWideChar(CP_UTF8, 0, szUtf8, -1, wszGBK, n);

	n = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
	if (n > Len)
	{
		delete[]wszGBK;
		return -1;
	}

	WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGbk, n, NULL, NULL);

	delete[]wszGBK;
	wszGBK = NULL;

	return 0;
}

4.判断字符串是否有非法字符

BOOL IsOnlyCharacterOrNum(CString str)//是否纯字母或数字
{
	for (int i = 0; i<str.GetLength(); i++)
	{
		if (str.GetAt(i)<'0' || (str.GetAt(i)>'9' && str.GetAt(i)<'A') || (str.GetAt(i)>'Z' && str.GetAt(i)<'a') || str.GetAt(i)>'z')
			return FALSE;
	}
	return TRUE;
}
BOOL IsOnlyNum(CString str)//是否纯数字
{
	for (int i = 0; i<str.GetLength(); i++)
	{
		if (str.GetAt(i)<'0' || str.GetAt(i)>'9')
			return FALSE;
	}
	return TRUE;
}

5.字符串分割

//分割字符串
int SplitString(CString str, CString split, CStringArray &strArray)//分割符为字符串split,str为被分割的字符串
{
	strArray.RemoveAll();
	CString strTemp = str;
	int iIndex = 0;
	while (1)
	{
		iIndex = strTemp.Find(split);
		if (iIndex >= 0)
		{
			strArray.Add(strTemp.Left(iIndex));
			strTemp = strTemp.Right(strTemp.GetLength() - iIndex - split.GetLength());
		}
		else
		{
			break;
		}
	}
	strArray.Add(strTemp);

	return strArray.GetSize();
}
int SplitString(const char* str, char split, CStringArray &strArray)//分割符为字符split,被分割字符串为char*型
{
	strArray.RemoveAll();
	CString strTemp(str);
	int iIndex = 0;
	while (1)
	{
		iIndex = strTemp.Find(split);
		if (iIndex >= 0)
		{
			strArray.Add(strTemp.Left(iIndex));
			strTemp = strTemp.Right(strTemp.GetLength() - iIndex - 1);
		}
		else
		{
			break;
		}
	}
	strArray.Add(strTemp);

	return strArray.GetSize();
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值