Reputation: 11399
I want to "validate" a wstring and remove undesired character.
This is what I would like to do:
wstring wsInput=L"Some Text $$!$§";
wstring wsNew=L"";
for (int i=0;i<wsInput.size();i++)
{
wstring wsChar=wsInput.CharacterAt(i);
wsChar = ToValidWString(wsChar); // ToValidWString will return L"" if the character is not among the valid characters
wsNew.append(wsChar);
}
return wsNew;
But there is no such function ".CharacterAt()" for wstring. I guess that is for a reason, but I need it anyway.
Can somebody help?
Thank you.
Upvotes: 2
Views: 2117
Reputation: 70969
You can use the operator []
. This returns a wchar
, not a string, but it seems to me this will make your code simpler. So to make things clearer:
for (int i=0;i<wsInput.size();i++)
{
wchar_t wc =wsInput[i]; // sorry for the name it comes from wchar ;)
... do stuff...
}
EDIT: to get a wstring consisting of the i-th character in the string use substr
:
for (int i=0;i<wsInput.size();i++)
{
wstring ws = wsInput.substr(i, 1);
... do stuff...
}
Upvotes: 3