如何获取StringBuffer中有多少个“\n”?

宁缺爱不滥情 2014-03-18 11:55:28
StringBuffer detailBuffer = new StringBuffer();

//迭代 bbbbDTOList
for (aaaaDTO fieldDTO :bbbbDTOList) {
LifeCycle oldLifeCycle = cmd.getOldLifeCycle();
LifeCycle newLifeCycle = cmd.getNewLifeCycle();
if (null == oldLifeCycle && null == newLifeCycle ) {
continue;
} else if (null == oldLifeCycle && null != newLifeCycle ) {
detailBuffer.append(" 添加:" + newLifeCycle + "\n");
} else if (null != oldLifeCycle && null == newLifeCycle ) {
detailBuffer.append(" 删除:" + oldLifeCycle + "\n");
} else if (!oldLifeCycle .equals(newLifeCycle )) {
detailBuffer.append(" 更改:" + newLifeCycle + "\n");
}
}
StringBuffer 中最后得到的数据 中是以“\n”隔开的!
现在我想统计 StringBuffer 中有多少个 “\n” 如何 实现??
...全文
356 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2014-03-18
  • 打赏
  • 举报
回复
StringBuffer detailBuffer = new StringBuffer();
		detailBuffer.append("sdsd\ndwewe\n32323\n333332");
		Pattern p = Pattern.compile("\n");
		int i = 0;
		Matcher m = p.matcher(detailBuffer.toString());
		while (m.find()) {
			i++;
		}
		System.out.println(i);
zark 2014-03-18
  • 打赏
  • 举报
回复
1 2 3 4 5
  • 打赏
  • 举报
回复
detailBuffer.append(" 删除:" + oldLifeCycle + "\n"); 用于 StringBuffer ,还在用字符串拼接,有啥意思么?
vcshcn 2014-03-18
  • 打赏
  • 举报
回复
从头到尾比较每一个byte值
Defonds 2014-03-18
  • 打赏
  • 举报
回复
用正则去匹配出来

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧