如何计算Java中字符串中每个字母的字母? [英] how to count the letters in each letter in a string in Java?

查看:76
本文介绍了如何计算Java中字符串中每个字母的字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用以下代码进行了尝试:

I have used this code to try this out:

String st="Hello world have a nice day";
String arr=st.Split(" ");
for (int i=0; i < arr.length; i++) {
    ???
}

但这不起作用。

我希望它输出如下内容:

I want it to output something like this:

Hello=5
World=5
have=4
a=1
nice=4
day=3

请问有人知道正确的代码吗?

Does anyone know the right code, please?

推荐答案

您可以使用以下代码:

String[] words = yourString.split(" ");

for (String word : words) {
  System.out.println(word + " length is: " + word.length());
}

这篇关于如何计算Java中字符串中每个字母的字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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