Java:如何找到给定字符串中最长的单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43251808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How to find the longest word in a given String?
提问by Suraj
In a given string, I want to find the longest word and print the same.
在给定的字符串中,我想找到最长的单词并打印相同的单词。
The output I get is the second longest word, i.e "Today"
, but I should get "Happiest"
.
May I know what I am doing wrong or is there a better/different way to find the longest word in a string?
我得到的输出是第二长的单词,即"Today"
,但我应该得到"Happiest"
.
我可以知道我做错了什么,还是有更好/不同的方法来找到字符串中最长的单词?
public class DemoString
{
public static void main(String[] args)
{
String s="Today is the happiest day of my life";
String[] word=s.split(" ");
String longword=" ";
for(int i=0;i<word.length;i++){
for(int j=1+i;j<word.length;j++){
if(word[i].length()>=word[j].length()){
longword=word[i];
}
}
}
System.out.println(longword+" is longest word with "+longword.length()+" characters");
System.out.println(rts.length());
}
}
回答by tt_emrah
instead it should be:
相反,它应该是:
for(int i=0 ; i<word.length ; i++)
{
if(word[i].length() >= rts.length())
{
rts = word[i];
}
}
回答by MSD
You can try like ,
你可以试试,
String s="Today is the happiest day of my life";
String[] word=s.split(" ");
String rts=" ";
for(int i=0;i<word.length;i++){
if(word[i].length()>=rts.length()){
rts=word[i];
}
}
System.out.println(rts);
System.out.println(rts.length());
回答by Madhavi
for(int i=0;i<word.length;i++){
for(int j=0;j<word.length;j++){
if(word[i].length()>=word[j].length()){
if(word[j].length()>=rts.length()) {
rts=word[j];
}
} else if(word[i].length()>=rts.length()){
rts=word[i];
}
}
}
回答by shash678
Here is a one-liner you can use with Java 8:
这是您可以在 Java 8 中使用的单行代码:
import java.util.*;
class Main {
public static void main(String[] args) {
String s = "Today is the happiest day of my life";
System.out.println(Arrays.stream(s.split(" ")).max(Comparator.comparingInt(String::length)).orElse(null));
}
}
Output:
输出:
happiest
Try it here!
回答by Vivekanandan V
String s= "Today is the happiest day of my life by vijayakumar";
String [] word = s.split(" ");
String maxlethWord = "";
for(int i = 0; i < word.length; i++){
if(word[i].length() >= maxlethWord.length()){
maxlethWord = word[i];
}
}
System.out.println(maxlethWord);
回答by Supun Dharmarathne
Try this one.
试试这个。
public static void main( String[] args )
{
String s = "Today is the happiest day of my life";
String[] word = s.split( " " );
String rts = " ";
for ( int i = 0; i < word.length; i++ )
{
if ( word[i].length() > rts.length() )
rts = word[i];
}
System.out.println( rts );
}
回答by TanvirChowdhury
// the below Java Program will find Smallest and Largest Word in a String
// 下面的 Java 程序将在字符串中找到最小和最大的单词
class SmallestAndLargestWord
{
static String minWord = "", maxWord = "";
static void minMaxLengthWords(String input)
{
// minWord and maxWord are received by reference
// and not by value
// will be used to store and return output
int len = input.length();
int si = 0, ei = 0;
int min_length = len, min_start_index = 0,
max_length = 0, max_start_index = 0;
// Loop while input string is not empty
while (ei <= len)
{
if (ei < len && input.charAt(ei) != ' ')
{
ei++;
}
else
{
// end of a word
// find curr word length
int curr_length = ei - si;
if (curr_length < min_length)
{
min_length = curr_length;
min_start_index = si;
}
if (curr_length > max_length)
{
max_length = curr_length;
max_start_index = si;
}
ei++;
si = ei;
}
}
// store minimum and maximum length words
minWord = input.substring(min_start_index, min_start_index + min_length);
maxWord = input.substring(max_start_index, max_length);
}
// Driver code
public static void main(String[] args)
{
String a = "GeeksforGeeks A Computer Science portal for Geeks";
minMaxLengthWords(a);
// to take input in string use getline(cin, a);
System.out.print("Minimum length word: "
+ minWord
+ "\nMaximum length word: "
+ maxWord);
}
}
**
**
Input : "GeeksforGeeks A computer Science portal for Geeks"
Output : Minimum length word: A
Maximum length word: GeeksforGeeks
**
**