如何找到w字符串中的字符总数?

问题描述 投票:0回答:1

我正在尝试找出输入字符串中的总字符数。当我输入字符串(一个句子)时,它仅计算字符串的第一个单词内的字符总数。我需要它来计算字符总数。我了解有很多方法可以做到这一点。但这是我的入门课程,也是我可以使用它的唯一方法string.length();。方法。

这是我所拥有的:

String shortSentence;
System.out.println("Please input a short sentence, with a period at the end.");
shortSentence = keyboardReader.next();
String length = shortSentence.length(); 
System.out.println("The total characters in the sentence is " + length); 
java string character counting
1个回答
0
投票

将长度转换为字符串:

String length = Integer.toString(shortSentence.length()); 
© www.soinside.com 2019 - 2024. All rights reserved.