0

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"

2 Answers 2

4

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("/*"); 
Sign up to request clarification or add additional context in comments.

1 Comment

But is TrimStart and TrimEnd the way to go? I simply want to trim the begining and ending of the string as stated above. I've done this easily in VBA, I can't believe this is such a beast! Like I said it's a simple console app, and I don't want MFC. Thank you.
1

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

1 Comment

Thank you. Enough yo get me started!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.