Home > Java > Javagetting Started > How to get the string length in java

How to get the string length in java

青灯夜游
Release: 2023-01-13 00:40:10
Original
33413 people have browsed it

In java, you can use the length() method of the String class to get the length of the string. The syntax format is "string name.length();"; this method can return the length of the string and the null character. The length of the string returns 0.

How to get the string length in java

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

In Java, to get the length of a string, you can use the length() method of the String class.

length() method is used to return the length of a string. The length of an empty string returns 0.

The syntax is as follows:

字符串名.length();
Copy after login

Return value: Returns the string length.

Example:

There are regulations for administrator passwords in the student information management system, that is, the password length must be greater than 6 characters and less than 12 characters. Because the password is too short and easy to be cracked, and if it is too long it is difficult to remember. This requires first obtaining the password string entered by the user, then calling the length() method to obtain the length, and then making further length judgments. The final implementation code is as follows:

public static void main(String[] args) {
    String sys = "学生信息管理";// 字义一个字符串表示系统名称
    System.out.println("欢迎进入《" + sys + "》系统");// 输出系统名称
    System.out.println("请设置一个管理员密码:");
    Scanner input = new Scanner(System.in);
    String pass = input.next();// 获取用户输入的密码
    int length = pass.length();// 获取密码的长度
    if (length > 6 && length < 12) {
        System.out.println("密码长度符合规定。");
        System.out.println("已生效,请牢记密码:" + pass);
    } else if (length >= 12) {
        System.out.println("密码过长。");
    } else {
        System.out.println("密码过短。");
    }
}
Copy after login

The above code saves the password entered by the user. to the string variable pass, then call the pass.length() method to save the length to the length variable, and then use the if statement to give a prompt based on the length.

Run the program. When the entered password is too short, the running result is as follows:

欢迎进入《学生信息管理》系统
请设置一个管理员密码:
123456
密码过短。
Copy after login

When the entered password meets the requirements, the running result is as follows:

欢迎进入《学生信息管理》系统
请设置一个管理员密码:
abc12345678
密码长度符合规定。
已生效,请牢记密码:abc12345678
Copy after login

Related video tutorial recommendations: Java video tutorial

The above is the detailed content of How to get the string length in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template