2015-12-31 40 views
0

问题我试图去掉名为“workorder”的字符串的所有“%”,出于某种原因,它不工作任何帮助将是非常非常感激!似乎无法让我的字符串剥离器在C++中正常工作

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

你有实际需要,在这里混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); 

请参阅t他docs here

相关问题