Tuesday, May 26, 2009

c++ : simple string substitution using find_first_of() and replace()

// loginURL: replace "id" with "auth" in hostname (so that "id.domain" becomes "auth.domain")
// tested and working!

string x = loginURL; 
size_t pos1 = x.find_first_of("//") + 2;
size_t pos2 = x.find_first_of(".");
size_t len = pos2-pos1;

x.replace(pos1, len, "auth");

No comments: