【问题标题】:Is there a way I can remove the range of characters form CString?有没有办法可以从 CString 中删除字符范围?
【发布时间】:2017-03-23 20:57:38
【问题描述】:

因为我们可以在 std::string 中使用 string.erase 函数来删除某些范围的字符,所以我可以使用任何函数来删除字符。

例如:

std::string string = "This is a test".

我可以使用

string.erase(2,(string.length()));

CString有类似的方法吗?

谢谢。

【问题讨论】:

  • 从未使用过它,但它有 LeftRight 并且可能也是连接这两个部分的一种方法
  • 查找CString的方法很痛苦,因为它有CSimpleStringCStringT的基类,而且方法很分散。
  • 如果您想删除字符串中某个术语的实例,您可以使用 Replace 并将该术语传递给它以查找和清空字符串。无论它出现在哪里,这基本上都会删除该术语。否则我会使用左右组合。

标签: string visual-c++ mfc erase


【解决方案1】:

CString::Left()CString::Right() 函数。您的代码的替代方案是:

CString result = string.Left(2);

或者如果你想删除中间的字符,你可以使用这样的东西:

CString result = string.Left(removeStart) + string.Right(removeEnd);

【讨论】:

    【解决方案2】:

    当然可以。看看CString Extraction Methods

    你的代码sn-p可以通过

    CString s = "This is a test".
    s = s.Left(2);
    ASSERT(s == "Th");
    

    要删除一系列字符以保留前 m 和后 n 个字符,您可以这样做:

    s = s.Left(m) + s.Right(n);
    

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 2019-12-21
      • 2014-06-02
      • 1970-01-01
      相关资源
      最近更新 更多