search
HomeJavaJavagetting StartedHow to get the string length in java

How to get the string length in java

Jul 13, 2021 pm 02:25 PM
javastring length

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();

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("密码过短。");
    }
}

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
密码过短。

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

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

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools