怎么判断两个cstring变量是否相等?在线等待!!!急!

alarm_zq 2002-06-06 11:36:58
比如:
CString str1,str2;
如何确定它们是否相等?
...全文
4834 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
akademecia 2002-06-17
  • 打赏
  • 举报
回复
== 可以的
happygirllei 2002-06-17
  • 打赏
  • 举报
回复
快给分!
林水福 2002-06-07
  • 打赏
  • 举报
回复
Comapre
mashimaro3600 2002-06-07
  • 打赏
  • 举报
回复
ai~~~
zzxcom 2002-06-07
  • 打赏
  • 举报
回复
lj0425(飞来的忧郁)
字符串操作不能使用这样的,
一定要用strcmp,判断返回的是0还是其他,
zzxcom 2002-06-07
  • 打赏
  • 举报
回复
lj0425(飞来的忧郁)
字符串操作不能使用这样的,
一定要用strcmp,判断返回的是0还是其他,
Tomorrow_zhf 2002-06-07
  • 打赏
  • 举报
回复
同意 jackstraw_lee(稻草人)
软猫克鲁 2002-06-07
  • 打赏
  • 举报
回复
应该可以吧,用==。
Jackstraw 2002-06-07
  • 打赏
  • 举报
回复
strcmp(str1,str2);
但是,注意str1,str2相等时,返回值是0,而不是TRUE
dycdyc123 2002-06-07
  • 打赏
  • 举报
回复
=======
devez 2002-06-07
  • 打赏
  • 举报
回复
在msdn上有,查一下就知道了.
有空好好看看msdn.
CString Comparison Operators
BOOL operator ==( const CString& s1, const CString& s2 );

BOOL operator ==( const CString& s1, LPCTSTR s2 );

BOOL operator ==( LPCTSTR s1, const CString& s2 );

BOOL operator !=( const CString& s1, const CString& s2 );

BOOL operator !=( const CString& s1, LPCTSTR s2 );

BOOL operator !=( LPCTSTR s1, const CString& s2 );

BOOL operator <( const CString& s1, const CString& s2 );

BOOL operator <( const CString& s1, LPCTSTR s2 );

BOOL operator <( LPCTSTR s1, const CString& s2 );

BOOL operator >( const CString& s1, const CString& s2 );

BOOL operator >( const CString& s1, LPCTSTR s2 );

BOOL operator >( LPCTSTR s1, const CString& s2 );

BOOL operator <=( const CString& s1, const CString& s2 );

BOOL operator <=( const CString& s1, LPCTSTR s2 );

BOOL operator <=( LPCTSTR s1, const CString& s2 );

BOOL operator >=( const CString& s1, const CString& s2 );

BOOL operator >=( const CString& s1, LPCTSTR s2 );

BOOL operator >=( LPCTSTR s1, const CString& s2 );

Return Value

Nonzero if the strings meet the comparison condition; otherwise 0.

Parameters

s1, s2

CString objects to compare.

Remarks

These comparison operators compare two strings. The operators are a convenient substitute for the case-sensitive Compare member function.

Example

The following example demonstrates the use of CString Comparison Operators.

// example for CString Comparison Operators
CString s1( "abc" );
CString s2( "abd" );
ASSERT( s1 < s2 ); // Operator is overloaded for both.
ASSERT( "ABC" < s1 ); // CString and char*
ASSERT( s2 > "abe" );

CString::Compare
int Compare( LPCTSTR lpsz ) const;

Return Value

Zero if the strings are identical, < 0 if this CString object is less than lpsz, or > 0 if this CString object is greater than lpsz.

Parameters

lpsz

The other string used for comparison.

Remarks

Compares this CString object with another string using the generic-text function _tcscmp. The generic-text function _tcscmp, which is defined in TCHAR.H, maps to either strcmp, wcscmp, or _mbscmp depending on the character set that is defined at compile time. Each of these functions performs a case-sensitive comparison of the strings, and is not affected by locale. For more information, seestrcmp, wcscmp, _mbscmp in the Run-Time Library Reference.

Example

The following example demonstrates the use of CString::Compare.

// example for CString::Compare
CString s1( "abc" );
CString s2( "abd" );
ASSERT( s1.Compare( s2 ) == -1 ); // Compare with another CString.
ASSERT( s1.Compare( "abe" ) == -1 ); // Compare with LPTSTR string.

还是自己去看看吧!
yg88 2002-06-07
  • 打赏
  • 举报
回复
为什么说用==不行?把代码贴出来看看。
happygirllei 2002-06-07
  • 打赏
  • 举报
回复
hehe,可以尝试一下用最原始(不过也是最保险最具通用性)的方法:

int len1=str1.length;
int len2=str2.length;
if(len1!=len2)
{
//肯定不相等
}
else
{
int i=1;
while(str1.substring(0,i)==str2.substring(0,i))&&(i<=len1)
{
i=i+1;
}
if(i==len1)
{
//相等
}
else
{
//不等
}
programe_ant 2002-06-07
  • 打赏
  • 举报
回复
大家好像对CString类还不了解,下面是msdn中的原文:

CString does not have a base class.

A CString object consists of a variable-length sequence of characters. CString provides functions and operators using a syntax similar to that of Basic. Concatenation and comparison operators, together with simplified memory management, make CString objects easier to use than ordinary character arrays.

CString is based on the TCHAR data type. If the symbol _UNICODE is defined for your program, TCHAR is defined as type wchar_t, a 16-bit character type; otherwise, it is defined as char, the normal 8-bit character type. Under Unicode, then, CString objects are composed of 16-bit characters. Without Unicode, they are composed of 8-bit char type.

When not using _UNICODE, CString is enabled for multibyte character sets (MBCS, also known as double-byte character sets, DBCS). Note that for MBCS strings, CString still counts, returns, and manipulates strings based on 8-bit characters, and your application must interpret MBCS lead and trail bytes itself.

CString objects also have the following characteristics:

CString objects can grow as a result of concatenation operations.


CString objects follow “value semantics.” Think of a CString object as an actual string, not as a pointer to a string.


You can freely substitute CString objects for const char* and LPCTSTR function arguments.


A conversion operator gives direct access to the string’s characters as a read-only array of characters (a C-style string).
Tip Where possible, allocate CString objects on the frame rather than on the heap. This saves memory and simplifies parameter passing.

CString assists you in conserving memory space by allowing two strings sharing the same value also to share the same buffer space. However, if you attempt to change the contents of the buffer directly (not using MFC), you can alter both strings unintentionally. CString provides two member functions, CString::LockBuffer and CString::UnlockBuffer, to help you protect your data. When you call LockBuffer, you create a copy of a string, then set the reference count to -1, which "locks" the buffer. While the buffer is locked, no other string can reference the data in that string, and the locked string will not reference another string. By locking the string in the buffer, you ensure that the string’s exclusive hold on the data will remain intact. When you have finished with the data, call UnlockBuffer to reset the reference count to 1.

For more information, see theStrings in MFC andStrings: Unicode and Multibyte Character Set (MBCS) Support articles in Visual C++ Programmer’s Guide andString Manipulation Routines in the Run-Time Library Reference.

#include <afx.h>

那是mfc中设计好的一个类,不同于char字符串!
memory_xj 2002-06-06
  • 打赏
  • 举报
回复
你可以用:
if (!str1.CompareNoCase(str2))
{
//相等
}
else
{
//不相等
}
alarm_zq 2002-06-06
  • 打赏
  • 举报
回复
我是这样用了, 可是就是不行 啊
zhangwei2001 2002-06-06
  • 打赏
  • 举报
回复
直接用比较符 ==
ttzzgg_80713 2002-06-06
  • 打赏
  • 举报
回复
也可以 == 呀。
ttzzgg_80713 2002-06-06
  • 打赏
  • 举报
回复
不是有个Comapre的函数吗?
small_wei 2002-06-06
  • 打赏
  • 举报
回复
str1.Compare(str2)==0
文件名: LedDisp.h 版本号: v1.0.5 (v20091122.00001) 功能: 显示七段数码管风格的数字、小数点、冒号及部分字母。 (此版本理论上可用于MFC,Win32 SDK或其它环境,在Windows XP sp2下用vc6.0及vs2008编译、运行通过.) 第一作者: Jef 日期: 20091122 电子邮件: dungeonsnd@126.com 地址: 中国/江苏 版权: 1.您可以修改及免费使用本程序,但把本程序或修改后程序用于商业用途前请先通知第一作者并得到作者的许可。 2.修改之后保留此文件开头处的文件说明信息并更改副版本号(如 v20090825.00001 改成 v20091002.00001 ), 并拷贝一份附上您的个人信息发送到上面的作者邮箱,作者负责在全面测试后发布您修改后的新版本。 3.您使用本程序而导致任何伤害以及经济损失,由过错方依法承担所有责任,一概与第一作者及合作单位无关。 4.如果您使用本程序则表示您已经同意此版本协议!否则请勿使用! 其它: v1.0.1 (v20090825.00001) v1.0.2 (v20091026.00001) 1.增加Hide()方法 2.增加IsHide()方法 3.增加Disp(CDC* pdc,CRect DispRect,int iDigitsAmount,CString cs);方法 4.等 v1.0.3 (v20091031.00001) 1.增加多个小数点和冒号显示支持 2.增加GetLedFont()方法 3.修改了显示熄灭数码管的内部实现 4.等 v1.0.4 (v20091105.00001) 1.修改了SetSegmentPosition(int index)函数,增加内部属性 修正因子m_bMakeAmend, 设置修改因子为真时,在特殊情况下(如用户设置段宽度为2个像素或者1个像素), 程序将保证所有段的宽度相等,并且保证段与段不会相连. 2.修改了保存显示区域背景机制! 前一版本在调用数码管显示函数(Disp()等函数)时会自动 保存一幅对应于显示区域RECT的位图,此种机制当用户在OnPaint()函数里调用显示 函数而不调用Hide()时,并且此时的RECT在每次调用显示函数时都改变,则会导致保存 背景位图的向量m_vecBkSave不断扩大,而实际上保存的位图将没有作用且浪费大量内存! 如: void CLEDView::OnPaint() { CPaintDC dc(this); OnShow(); //不断的以不同的RECT区域调用Disp()将导致保存的 //位图(对应于每个区域)数量剧增! } void CLEDView::OnShow() { CClientDC dc(this); CRect rtClient; GetClientRect(&rtClient); double m_x =rtClient.Width()/800.0; double m_y =rtClient.Height()/600.0; CRect rt0; rt0.SetRect(20*m_x,10*m_y,220*m_x,410*m_y); led.Disp((CDC*)&dc,rt0,1,_T("1:")); } 解决方案: 保存位图将不再以RECT来标识,而是以一个用户输入的字符串来标识. 3. 添加MFC之外框架的支持.(未完成,未测试,希望测试的用户给作者联系测试结果.如WIN32 SDK,WTL等环境下.) v1.0.5 (v20091122.00001) 1. 增加UNICODE支持 2.完成MFC外的某些框架的支持 3.修改了和完善了某些代码段。如 删除保存背景的 向量某一元素之前先把其中的位图删除; 内存dc用完之后添加删除操作 DeleteDC ; 构析函数中增加了删除 全部位图资源操作 4. 修改了数个变量名使其更统一,如 SHOWPOS改成了iShowPos; 删除了某些接口,如 DispInt(); 2.说明及注意 (Attention!) a.支持 改变数码管亮时的颜色 灭时颜色 区域背景色,支持居左居中居右显示,支持细调显示的位置(SetFont();)及数码管粗细等细节. 支持距离左或右一定距离显示字符串(m_iDistaceToRight). 支持创建不定数量的自定义字体(LedFontID来表示已创建的不同字体,已经创建过的字体被再次创建时则改写这个字体). 支持默认参数(调用时可以依次不传递有默认值参数). b.显示的字符的宽度与高度不随窗口的大小变化而自动变化,故调用者应自行处理. c.内部不含自动重画功能,故调用者应自己处理窗口重绘时字符的重新显示. d.字符串内可同时含 ":"与"." ,但字符串左起第一个字符不能为":"或"." ,且不能连续两个不点位符号,如小数点或冒号!! e.能显示的字符包括 0-9,a-f,A-F,".",":","-" f.如 bool Disp(HDC hDc,RECT rtDispRect,TCHAR* cs,int iFontWidth,int iFontHeight,TCHAR* strIdentifier=_T("LedValue1")); 用户应自己设置好 rtDispRect与iFontWidth iFontHeight 的大小关系. 如果iFontHeight大于rtDispRect.Height(),则超出DispRect区域显示字符. 但左右可能不会超出显示(用户可设置m_bShowWhenOverstepRect来控制是否显示). 调用者应自己保证使字符串能全部显示在DispRect中,否则超出部分可能不会显示!!! g.当字符的宽度与数码管的每一段的宽度比例严重失调时,显示的字符会严重失真(如数码管每一段宽度为5个像素, 而长度却也为5像素则显示效果将失真)! 一般情况下,iFontHeight=2*m_iFontWidth,m_dSegmentWidth远小于m_iFontWidth h. led.SetColor(RGB(0,200,0)) 等方法会改变类的字体属性,故之后调用显示时字体属性都会发生改变. 但创建新的字体时不会改变类的字体属性. i.调用 Disp(...,strIdentifier)等此类数码管显示函数显示数码管后, 显示区域的背景及区域会自动保存. 1.再次调用Disp(...,strIdentifier)时会用新的显示区域的背景替换原背景及区域; 2.调用Hide(...,strIdentifier)方法时程序会自动删除strIdentifier对应原位图及区域,然后隐藏该区域的数码管(用原背景位图贴在此区域); 3.对象析构时会自动删除已经保存的所有的背景位图及区域. 3.使用方法(仅举一种使用方法) (Using e.g.) i. void CLEDView::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here OnShow(); // Do not call CView::OnPaint() for painting messages } void CLEDView::OnShow() { // TODO: Add your command handler code here CClientDC dc(this); CRect rtClient; GetClientRect(&rtClient); CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP1); CDC memdc; memdc.CreateCompatibleDC(&dc); memdc.SelectObject(&bmp); dc.StretchBlt(0,0,rtClient.Width(),rtClient.Height(),&memdc,0,0, 800,600,SRCCOPY); double m_x =rtClient.Width()/800.0; double m_y =rtClient.Height()/600.0; CRect rt0; rt0.SetRect(int(20*m_x),int(10*m_y),int(420*m_x),int(80*m_y)); CString s; s ="123-:1:."; TCHAR str[256]; sprintf(str,"%s",s); led.DispDigits((HDC)dc,rt0,str,7,_T("Led1")); } void CLEDView::OnHide() { // TODO: Add your command handler code here CClientDC dc(this); led.Hide((HDC)dc,"Led1"); } ii. CClientDC dc(this); ...... CRect rt0; rt0.SetRect(int(20*m_x),int(10*m_y),int(420*m_x),int(80*m_y)); CString s; s ="123-:1:."; led.Disp_mfc((CDC*)&dc,rt0,s,27,45,_T("Led1")); */

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧