专业的编程技术博客社区

网站首页 > 博客文章 正文

找出最长字符串(找出最长字符串的方法)

baijin 2024-08-13 00:53:06 博客文章 7 ℃ 0 评论

题目描述

求出5个字符串中最长的字符串。每个字符串长度在100以内,且全为小写字母。

输入样例

one two three four five

输出样例

three

代码实现

//求出5个字符串中最长的字符串。每个字符串长度在100以内,且全为小写字母
import java.util.Scanner;
public class chartExel {
    public static void main(String[] args) {
        //输入五个字符串(长度为 100以内)
        Scanner scanner=new Scanner(System.in);
        String input=scanner.nextLine();

        //one two three four five
        //正则表达式,split将字符串分割成字符串数组
        //String str[]=string.split(",|\\s|!");
        String str[]=input.split(" ");

        String maxWord=str[0];
        for (int i=0;i<str.length;i++){
            //System.out.println(str[i]);
            if(str[i].length() > maxWord.length()){
                maxWord=str[i];
            }
        }
        System.out.println(maxWord);
    }
}

知识点

字符串方法split()根据给定正则表达式的匹配拆分此字符串。

遍历数组

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表