PDA

View Full Version : Replacing polish diacritics in the string



kornicameister
8th February 2011, 14:20
What would you propose if I said that I need to the replace polish diacritics in the string.

I was thinking of using const array of these chars
and iterating over the string (or using regexp) and replacing found diacritics with their equivalents.
for example string like this "koło" should be passed further as "kolo" which means that character on the 3 position ł should be replaced by l?

or

preparing simple QValidator but I never used that :). I found it has member setLocale() which might be useful.

What would you suggest and what are your opinions about my ideas :)

stampede
8th February 2011, 15:19
First solution that comes to my mind is to create a QMap<QChar,QChar> with polish diactrics <-> desired characted mapping, and use QString::replace() :

void removeDiactrics( QString * str ){
foreach( QChar c, _diactrics.keys() ){
str->replace(c, _diacrtics[c]);
}
}

This is maybe not the best solution for this problem, and I'd really like to see some comments on this from others.

QValidator is ok if you want to check the string input while user is typing, for example in QLineEdit, and QLocale is used for string <-> numbers conversions in different languages, so I dont see how this may help here. Maybe I undestood you wrong, but I was thinking about diacrtics replacement in any QString object anywhere in program.

kornicameister
8th February 2011, 15:29
to be precise I need everywhere user wants to search for something in the database
and he is typing a criteria.

No matter what he has typed (big letters mixed with small letters, polish diacritics or no polish diacritics ) the searching must return valid results as the db holds rough data.

Solution you came up with is better than mine, and thanks, because If I got you correctly what you propose is to have map where keys are polish diacritics, right ?
It is better than const array, however I will make this map constant.

One problem left is if there is a need to encapsulate such simple functionality in the class. But I think I will do so, which will leave the open way to maybe extend it

stampede
8th February 2011, 15:54
Great I could be of any help :)

map where keys are polish diacritics
Yes, exactly.