c++ - 从 System::String 中删除除数字和小数点以外的所有内容

标签 c++ c++-cli

我有一个 System::String(在 C++ CLI windows 窗体应用程序中),我想对其进行格式化以删除除数字和小数点以外的所有内容。

字符串应该看起来像 "letters0.231 letters $#symbols" 我只想保留 0.231

我不想将这个 String 编码为 std::string 并使用 erase() 函数,然后必须转换回 System::String

有没有办法直接在 System::String 上执行此操作?

最佳答案

我认为您可以使用正则表达式。作为

String ^s = "letters0.231 letters $#symbols";
Regex ^regex = gcnew Regex( "\\d*\\.\\d*" );

s = regex->Match( s )->Value;
double d = Double::Parse( regex->Match( s )->Value );

Console::WriteLine( "s = {0}", s );
Console::WriteLine( "d = {0}", d );

关于c++ - 从 System::String 中删除除数字和小数点以外的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20103689/

相关文章:

c++ - 如何将信号作为函数参数传递?

C++ 动态数组访问冲突

c++ - 未处理的异常 : System. AccessViolationException:试图读取或写入保护

c++ - 了解opencv中的Moments函数

c++ - base 和 derived 之间的 "IS A"关系是什么意思?

c++ - 为并行化的 for() 循环提供线程私有(private)预分配缓冲区?

c# - VS2010 C++/CLI 项目总是认为 VS2012 C# 项目已过时

.net - 如何集成/混合托管和非托管代码

c++ - Marshal::用 CvMat 复制

c++ - "#using"需要 C++/CLI 模式