字符串中寻找汉字

我们都知道,字符串中的每个字符都占一个字节,但是汉字不同,它占两个字节。而且第一个字节的高位为1,第二个字节的高位也为1.我们可以利用这个特点来判断字符串中是否含有汉字。

代码实现:

bool IsTrue(char* p)
{
	while(*p)
	{
		if((*p&0x80) && (*(p+1)&0x80))//第一个和第二个字节的高位都为1
		{
			return true;
		}
		else
			p++;
	}
	return false;

}
int main()
{
	char *str = "inhgxj我kjh";
	bool ret = IsTrue(str);
	if(ret == true)
	{
		cout<<"have"<<endl;//存在汉字
	}
	else
	{
		cout<<"no have"<<endl;//不存在汉字
	}
}

测试结果:

时间: 2024-08-29 21:25:28

字符串中寻找汉字的相关文章

C#编程入门--将指定字符串中的汉字转换为拼音缩写,其中非汉字保留为原字符

将指定字符串中的汉字转换为拼音缩写,其中非汉字保留为原字符 #region 将指定字符串中的汉字转换为拼音缩写,其中非汉字保留为原字符 /// <summary> /// 将指定字符串中的汉字转换为拼音缩写,其中非汉字保留为原字符 /// </summary> /// <param name="text"></param> /// <returns></returns> public static string G

C++搜索字符串中的汉字

示例:返回输入字符串中汉字的个数: int GetChineseCharacterCount(char *pStr) { int retCnt = 0; int i=0; while(pStr[i]!=0) { if(pStr[i] & 0x80) { retCnt++; i++;    // 因为一个汉字两个字节 } i++; } return retCnt; } 以下收自: http://blog.163.com/[email protected]/blog/static/791554782

正则表达式 exec 获取字符串中的汉字

要求:仅获取attr中的 “编辑发起状态的执行人表单” ,路径C:\fakepath\是不固定的,可以是C:\fakepath\hhh\hhhh\ 解决: var attr = C:\fakepath\编辑发起状态的执行人表单.png 1 attr = title.split(".")[0]; // 截取到 ——> "C:\fakepath\编辑发起状态的执行人表单" 2 var reg = new RegExp('[\u4e00-\u9fa5]+$','g'

C++实现在一个字符串中寻找最大子串

//#include "stdafx.h" #include<iostream> #include<vector> #include<string> #include<utility> using namespace std; pair<int ,string> fun(const string &s) { vector<string> substrs; string substr; int length=

在字符串中寻找目标字符串

在终端输入多行信息,找出包含"ould"的行,并打印改行. 如: Au,love could you and I with fate conspire To grasp this sorry scheme of things entire, Would not we shatter it to bitd – and then. 在终端输出上述的文字,输出 Au,love could you and I with fate conspire Au,love could you and I

【lua】lua string.match 和 string.split 从字符串中寻找特定字符串并保存

local string = "{1,2,3,4}" local traString=string.match(string , "%d+,%d+,%d+,%d+") --此时tranString = "1,2,3,4",去掉"{","}" string = string.split(tranString , ",") string = {1,2,3,4} string[1]=1 str

php 正则获取字符串中的汉字preg_match_all

preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $list[$i]['iparr'], $matches); $list[$i]['iparr'] = join('', $matches[0]);

在一个字符串中寻找某个字串

1 public int strStr(String haystack, String needle) { 2 for(int i = 0; ; i++) { 3 for(int j = 0; ; j++) { 4 if(j == needle.length()) { 5 return i; 6 } 7 8 if(i+j == haystack.length()) { 9 return -1; 10 } 11 12 if(needle.charAt(j) != haystack.charAt(i

java-统计字符串中的汉字个数

1 String text = "你好,,..wo"; 2 String Reg="^[\u4e00-\u9fa5]{1}$";//正则 3 int result=0; 4 for(int i=0;i<text.length();i++){ 5 String b=Character.toString(text.charAt(i)); 6 if(b.matches(Reg)) 7 result++; 8 } 9 } 原文地址:https://www.cnblog