关于字符串比较stricmp的问题 [英] question about string compare stricmp
问题描述
我想比较两个字符串,无论是小写还是大写。
例如,txt与TXT,Txt相同,......
我知道某些c ++中的stricmp可以执行小写
比较。但是当我使用< cstring>时,我找不到这样的功能。你有没有知道任何其他标准的c ++函数可以完成这项任务吗?
谢谢,
X
Hi,
I want to compare two strings regardless of the lowercase or uppercase.
For example, "txt" same as "TXT", "Txt", ...
I know that there''s stricmp in some c++ that can perform a lowercase
comparison. But when I use <cstring>, I can''t find such function. Do you
know any other standard c++ function(s) can do this task?
Thanks,
X
推荐答案
xuatla写道:
我想比较两个字符串而不管小写字母或大写。
例如,txt。与TXT,Txt相同,......
我知道某些c ++中的stricmp可以执行小写的比较。但是当我使用< cstring>时,我找不到这样的功能。你知道其他标准的c ++函数可以完成这个任务吗?
谢谢,
X
Hi,
I want to compare two strings regardless of the lowercase or uppercase.
For example, "txt" same as "TXT", "Txt", ...
I know that there''s stricmp in some c++ that can perform a lowercase
comparison. But when I use <cstring>, I can''t find such function. Do you
know any other standard c++ function(s) can do this task?
Thanks,
X
我认为你必须自己动手。
I think you have to roll your own.
首先使用c_str()然后使用strcmp怎么样?
http://www.cppreference.com/cppstring/c_str.html
i很快写了一些东西来测试这个myelf(希望这是
正确的方法):
#include< iostream>
#include< string>
使用命名空间std;
int main()
{
string test1 =" abcd";
string test2 =" abCd";
cout< ;< test1<< " " << test2<< " " << strcmp(test1.c_str(),
test2.c_str())<< endl;
test2 =" ABCD";
cout<< test1<< " " << test2<< " " << strcmp(test1.c_str(),
test2.c_str())<< endl;
test2 =" abcd";
cout<< test1<< " " << test2<< " " << strcmp(test1.c_str(),
test2.c_str())<< endl;
test2 =" dcba";
cout<< test1<< " " << test2<< " " << strcmp(test1.c_str(),
test2.c_str())<< endl;
test2 =" abcdE";
cout<< test1<< " " << test2<< " " << strcmp(test1.c_str(),
test2.c_str())<<结束;
返回0;
}
输出
--- -------------------
abcd AbCd 1
abcd ABCD 1
abcd abcd 0
abcd dcba -1
abcd abcdE -1
how about using c_str() first and then using strcmp?
http://www.cppreference.com/cppstring/c_str.html
i quickly wrote something to test this myelf (hopefully this is the
correct way to do this):
#include <iostream>
#include <string>
using namespace std;
int main()
{
string test1 = "abcd";
string test2 = "AbCd";
cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
test2.c_str()) << endl;
test2 = "ABCD";
cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
test2.c_str()) << endl;
test2 = "abcd";
cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
test2.c_str()) << endl;
test2 = "dcba";
cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
test2.c_str()) << endl;
test2 = "abcdE";
cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
test2.c_str()) << endl;
return 0;
}
output
----------------------
abcd AbCd 1
abcd ABCD 1
abcd abcd 0
abcd dcba -1
abcd abcdE -1
xuatla写道:
xuatla wrote:
我想比较两个字符串,无论是小写还是大写。
例如,txt与TXT,Txt相同,......
我知道某些c ++中的stricmp可以执行小写的比较。但是当我使用< cstring>时,我找不到这样的功能。你知道其他标准的c ++函数可以完成这个任务吗?
谢谢,
X
Hi,
I want to compare two strings regardless of the lowercase or uppercase.
For example, "txt" same as "TXT", "Txt", ...
I know that there''s stricmp in some c++ that can perform a lowercase
comparison. But when I use <cstring>, I can''t find such function. Do you
know any other standard c++ function(s) can do this task?
Thanks,
X
这种比较取决于您的语言环境。以下代码默认为
全局语言环境std :: locale()。
#include< locale>
# include< string>
#include< iostream>
template< typename CharIter>
bool
sequence_equal_to_ignoring_case(CharIter a_from,CharIter a_to,
CharIter b_from,CharIter b_to,
std :: locale const& loc = std :: locale()){
if(std :: distance(a_from,a_to)
!=
std :: distance(b_from,b_to)){
返回(false);
}
for(CharIter a_iter = a_from,b_iter = b_from;
a_iter!= a_to;
++ a_iter,++ b_iter){
if(std ::) tolower(* a_iter,loc)!= std :: tolower(* b_iter,loc)){
return(false);
}
}
返回(true);
}
bool
string_equal_to_ignoring_case(std :: string const& a,
std :: string const& b,
std :: locale const& loc = std :: locale()){
return(sequence_equal_to_ignoring_case(a.begin(),a.end(),
b.begin(),b.end(),
loc));
}
int main(void){
std :: string a(" Hello World!" );
std :: string b(" hello world!");
std :: cout<< string_equal_to_ignoring_case(a,b)<< ''\ n'';
}
最好
Kai-Uwe Bux
ps。:这不是常见问题解答吗?它应该在FAQ中吗?
Such comparisons depend on your locale. The following code defaults to the
global locale std::locale().
#include <locale>
#include <string>
#include <iostream>
template < typename CharIter >
bool
sequence_equal_to_ignoring_case ( CharIter a_from, CharIter a_to,
CharIter b_from, CharIter b_to,
std::locale const & loc = std::locale() ) {
if ( std::distance( a_from, a_to )
!=
std::distance( b_from, b_to ) ) {
return ( false );
}
for ( CharIter a_iter = a_from, b_iter = b_from;
a_iter != a_to;
++ a_iter, ++b_iter ) {
if ( std::tolower( *a_iter, loc ) != std::tolower( *b_iter, loc ) ) {
return ( false );
}
}
return ( true );
}
bool
string_equal_to_ignoring_case ( std::string const & a,
std::string const & b,
std::locale const & loc = std::locale() ) {
return( sequence_equal_to_ignoring_case( a.begin(), a.end(),
b.begin(), b.end(),
loc ) );
}
int main ( void ) {
std::string a ( "Hello World!" );
std::string b ( "hello world!" );
std::cout << string_equal_to_ignoring_case( a, b ) << ''\n'';
}
Best
Kai-Uwe Bux
ps.: Isn''t this in the FAQ? and should it be in the FAQ?
这篇关于关于字符串比较stricmp的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!