How to convert “\u002f” to “/” (in c++)?

做~自己de王妃 提交于 2021-02-08 06:13:22

问题


I have to following string which i get from share point : \u002fsites\u002fblabla\u002fShared Documents\u002fkittens.xml

and i'm trying to convert it to : /sites/blabla/Shared Documents/kittens.xml

I googled it and found that it is Unicode encoded, but i couldn't find anything that converts it, technically i can write a small function that converts all the "\u002f" to "/" but i don't think it is the right thing to do.

If any one can shed some light on this matter it would be very helpful.

Thanks


回答1:


I found a library that solved my problem, and i want to share the solution in case someone else will stumble upon this question.

The library called ICU

And the use is very simple :

icu::UnicodeString converter = icu::UnicodeString(in.c_str(),in.length());
icu::UnicodeString newUstring = converter.unescape();
newUstring.toUTF8String(out);

when in and out are string.




回答2:


I don't whether there's a ready function in C++ to do that or not , but you can do that by this algorithm :

1-find "\" character in your first string using find function in string class,change it to "/" 2- omit from this point to 4 nexts(incluse u002) and so on...

#include <iostream>
#include <string>
using namespace std;
string process(string str){
    string result="";
    for (int i=0;i<str.size();i++){
        if(str[i]=='\\' ) {
            result.push_back('//');
            i+=5;
        }
        else
            result.push_back(str[i]);
    }
    return result;
}
int main(){
    string path;
    getline(cin,path);
    cout<<process(path)<<endl;
}



回答3:


If the string is a string literal, the compiler should take care of this translation; a sequence \uxxxx is a "universal character name". If it's data you're reading from an outside source, I'm not aware of any standard function which would convert it; you'll have to write your own. (And getting it right isn't that trivial, once you add all of the necessary error checking.)



来源:https://stackoverflow.com/questions/23237685/how-to-convert-u002f-to-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!