如何计算字符串中每个字的字符串java [英] how to count the letters in each word in a string java

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

问题描述

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

我需要这样的:

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


推荐答案

您可以使用这样的:

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

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

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

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