似乎无法让我的字符串剥离器在c ++中正常工作

时间:2015-12-31 12:54:46

标签: visual-c++ c++-cli

我的问题是我试图删除一个名为“workorder”的字符串的所有“%”,并且由于某种原因它不起作用任何帮助将非常感激!

example:

String ^ workorder = "%QW1234%12%3"
with the below code I want it to spit out the workorder string like so = "QW1234123" 

这是我的代码

private: System::Void workorder_text_TextChanged(System::Object^  sender, System::EventArgs^  e) {
         String ^ workorder;
         workorder = workorder_text->Text;

         //I CANT USE WORKORDER STRING FOR wO string for some reason....

         string wO(workorder);

         char bad_chars_wo[] = "%";
         for (unsigned int i = 0; i < strlen(bad_chars_wo); ++i)
         {
         wO.erase (std::remove(wO.begin(), wO.end(), bad_chars_wo[i]), wO.end());
         }
     }

1 个答案:

答案 0 :(得分:1)

您是否真的需要在此处混合使用System::Stringstd::string个对象(例如,混合使用CLI字符串和C ++字符串)?

解决问题的最简单方法是使用System::String提供的方法:

auto workorder = workorder_text->Text;
workorder = workorder->Replace("%", String::Empty);

如果您确实需要std::string以供日后处理,则可以编组 System::String

#include <msclr/marshal_cppstd.h>
auto wO = msclr::interop::marshal_as<std::string>(workorder);

请参阅docs here