ravi
ravi

Reputation: 277

String Comparision without Special Character

I have strings having special characters e.g.

 "ravi", "Ravi" ,"!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.

Upvotes: 1

Views: 3579

Answers (3)

Bohemian
Bohemian

Reputation: 425043

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"

Upvotes: 3

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

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

Upvotes: 0

pmg
pmg

Reputation: 108938

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++;
}

Upvotes: 5

Related Questions