Pergunta

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.

Foi útil?

Solução

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...
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top