Java中的String类中没有反向方法? [英] No reverse method in String class in Java?

查看:41
本文介绍了Java中的String类中没有反向方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Java的String类没有反向方法?取而代之的是 reverse() 方法在 StringBuilder?是否有一个原因?但是String有split()regionMatches()等,比reverse()方法复杂.

Why there is no reverse method in String class in Java? Instead, the reverse() method is provided in StringBuilder? Is there a reason for this? But String has split(), regionMatches(), etc., which are more complex than the reverse() method.

当他们添加这些方法时,为什么不添加reverse()?

When they added these methods, why not add reverse()?

推荐答案

既然你在 StringBuilder 中有它,那么在 String 中就不需要它了,对吧?:-)

Since you have it in StringBuilder, there's no need for it in String, right? :-)

说真的,在设计 API 时,您可以包含很多东西.然而,为了简单和清晰,界面有意保持较小.谷歌上API 设计",你会发现 页面 同意这一点.

Seriously, when designing an API there's lots of things you could include. The interfaces are however intentionally kept small for simplicity and clarity. Google on "API design" and you'll find tons of pages agreeing on this.

如果你真的需要它,你可以这样做:

Here's how you do it if you actually need it:

str = new StringBuilder(str).reverse().toString();

这篇关于Java中的String类中没有反向方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆