开发者

String Comparision without Special Character

I have strings having special characters e.g.

 "ravi", "Rav开发者_StackOverflowi" ,"!ravi","ravi...","RaVi)" etc..

I want all these to be treated as same. How to achieve this. Can be in shell script, C,C++,JAVA.

Thanks, Ravi.


In C I'd use isalpha in a loop, removing the unwanted characters before treating the string.

#include <ctype.h>

/* ... */

loop {
  if (isalpha((unsigned char)*src)) *dst++ = *src;
  src++;
}


Java: str.toLowercase().replace("[^a-zA-Z]", "") will render them all in lowercase with special chars removed and therefore all equal()

Edited to cover everything not alpha as "special"


Use find function of String in c++ to find the occurrence of your substring in your case "ravi".

size_t find ( const string& str, size_t pos = 0 ) const;

It will return you starting index of your substring in main string.

by using that information, you could manipulate the string as per your requirement.

More on string::find

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜