Windows 提供的两种字符串比较函数,CompareString、CompareStringOrdinal

我们常会比较两个字符串的大小,C++中,strin提供了strcomp函数用于比较ANSI编码下的字符串大小,Windows下会存在Unicode编码,所以提供CompareString、CompareStringOrdinal这两个函数。

#include <iostream>
#include "Windows.h"
using namespace std;

int main()
{
	::string str1 = "123";
	::string str2 = "345";
	int l=strlen(str1.c_str());
	::wstring wstr1 = L"123";
	::wstring wstr2 = L"345";
	int l2 = wcslen(wstr1.c_str());
	//获取当前线程的LCID
	LCID m_Id=GetThreadLocale();
	//以符合用户语言习惯的方式向用户显示的字符串,进行比较
	DWORD wRet=CompareStringA(m_Id,SORT_STRINGSORT,str1.c_str(),strlen(str1.c_str()), str2.c_str(),strlen(str2.c_str()));
	//比较程序内部字符串(如路径名、注册表项/值、XML元素/属性等) 
	DWORD wRet1 = CompareStringOrdinal(wstr1.c_str(), lstrlen(wstr1.c_str()), wstr2.c_str(), lstrlen(wstr2.c_str()),true);
	system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值