4

Possible Duplicate:
Platform::String is kind of useless

I'm new to Windows development and the new Visual C++ APIs are driving me nuts. I've most recently run into a seeming lack of string functions. Are there basic functions available such as:

  • substring
  • strpos or similar
  • regular expressions

My ultimate goal is to take a file path such as "C:\foo\bar\baz.jpg" and extract the deepest directory. In this example, I'm looking for "bar".

Perhaps of a greater concern, I've found that it's incredibly difficult to find current documentation for Win 8 APIs. What's the best place to look for questions like this?

Community
  • 1
  • 1
Brent Traut
  • 5,614
  • 6
  • 29
  • 54
  • 1
    I retagged your question, removing the "wpf" tag and adding the "c++-cx" tag (since `Platform::String` is a C++/CX class). – Mr.C64 Oct 18 '12 at 10:47

2 Answers2

3

substring is part of the standard C++ library.

string x = "abc";
string y = x.substr(1, 2);

Not completely sure I know what strpos does, but assuming it's about finding the position of a character in a string or similar then it's also standard.

string x = "abc";
string::size_type p = x.find('b');

Regexes can be found in the cross platform boost library, http://boost.org.

john
  • 7,897
  • 29
  • 27
  • 2
    Regexes are also part of C++11 – Andreas Brinck Oct 18 '12 at 10:01
  • I need a property on a class that is bindable, so I've been using platform::string. Should I use standard C++ strings until I have what I need and then create a platform::string afterwards? Or am I misunderstanding some other aspect here? – Brent Traut Oct 18 '12 at 10:13
  • And to find the _last_ occurrence of a string/character, there is [`rfind`](http://en.cppreference.com/w/cpp/string/basic_string/rfind). – Some programmer dude Oct 18 '12 at 10:17
  • 1
    C++/CX's `Platform::String` uses _Unicode_, so - at least in MS Visual C++ - you should use `std::wstring` as corresponding C++ class (not `std::string`). – Mr.C64 Oct 18 '12 at 10:43
3

WinRT C++/CX classes like Platform::String should be used only at the boundary of your apps/components. Inside your apps/components you should just use ordinary ISO C++ classes.

So you can just use std::wstring (since Platform::String is Unicode) with its methods and Boost's helpers, and then convert from/to std::wstring to/from Platform::String at the boundary.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • Aha! This was my biggest confusion. Thanks a ton for helping me clear it up. – Brent Traut Oct 18 '12 at 10:43
  • For others wondering how to do what Mr.C64 suggests, this site has a lot of great info: [Platform::String, std::wstring, and Literals](http://msdn.microsoft.com/en-us/library/hh438486(v=vs.110).aspx) – Brent Traut Oct 18 '12 at 11:15