ME-dia
ME-dia

Reputation: 289

Trimming a String

I'm using windows 7 and Visual C++. I have a console program and I am trying to trim a string at the begining and the end. TrimLeft() and TrimRight() don't seem to work without MFC. Here is what I have so far.

pBrowser->get_LocationURL(&bstr);
wprintf(L"  URL: %s\n\n", bstr);
SysFreeString(bstr);

std::wstring s;
s = bstr;
s.TrimStart("http://");
s.TrimEnd("/*");
wprintf(L"  URL: %s\n\n", s);

I'm trying to go from this:

"http://www.stackoverflow.com/questions/ask"

to this:

"www.stackoverflow.com"

Upvotes: 0

Views: 477

Answers (2)

stefan
stefan

Reputation: 2886

You should use find/rfind(right find - find from right) and substr(sub string) in sequence to do what you need to do. 1) Find the index of the first pattern (such as http://) with find - you already know its length, add this to the start index as the origo of your trimmed string 2) Find the last index of the ending pattern with find

3) Create a substring from the origo to the end using substr

These methods are all in std::string

Upvotes: 1

twinkie
twinkie

Reputation: 41

TrimStart/End usually return a value, so you would have to set 's' to equal the value of s.TrimStart() and s.TrimEnd() respectively.

try,

s = s.TrimStart("http://");
s = s.TrimEnd("/*");

Upvotes: 4

Related Questions