【问题标题】:How to use replaceAll() method in StringBuffer?如何在 StringBuffer 中使用 replaceAll() 方法?
【发布时间】:2016-02-26 03:11:03
【问题描述】:

我需要替换字符串缓冲区中的多个单词。所以我在StringBuffer中寻找replaceAll方法。

那么我们在StringBuffer 中有它吗?

字符串方法:

str2 = str1.replaceAll(regex, substr);
// (This is String method, I need like this in StringBuffer)

【问题讨论】:

  • 为什么一定要在StringBuffer做呢? stringBuffer.toString().replaceAll(regex, substr); 不工作吗?

标签: java stringbuffer


【解决方案1】:

StringBuffer 上没有这种方法。也许以下内容会有所帮助:

StringBuffer str1 = new StringBuffer("whatever");
// to get a String result:
String str2 = str1.toString().replaceAll(regex, substr);
// to get a StringBuffer result:
StringBuffer str3 = new StringBuffer(str2);

【讨论】:

  • 很高兴它有帮助!您还可以将此答案标记为已接受(使用绿色复选标记)和/或将其标记为有用(使用向上箭头):-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
相关资源
最近更新 更多