Home > Java > javaTutorial > Use java's String.length() function to get the length of a string

Use java's String.length() function to get the length of a string

PHPz
Release: 2023-07-25 09:09:06
Original
4299 people have browsed it

Use Java's String.length() function to get the length of a string

In Java programming, strings are a very common data type. We often need to get the length of a string, that is, characters The number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string.

The following is a simple sample code:

public class StringLengthExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int length = str.length();
        System.out.println("字符串的长度为:" + length);
    }
}
Copy after login

In the above sample code, we define a string variable str and initialize it to "Hello, World !". Then we use the str.length() function to get the length of the string and assign the result to the integer variable length. Finally, we use the System.out.println() function to output the length of the string to the console.

Run the above code, the output result is:

字符串的长度为:13
Copy after login

As can be seen from the output result, the length of the string "Hello, World!" is 13, that is, the string contains 13 characters .

It should be noted that the length of a string refers to the number of characters in the string, not the number of bytes in the string. In Java, a Chinese character usually occupies two bytes of memory space. If you want to get the number of bytes in a string, you can use the getBytes() function of the String class.

The following is a sample code to get the number of bytes in a string:

public class StringByteLengthExample {
    public static void main(String[] args) {
        String str = "你好,世界!";
        byte[] bytes = str.getBytes();
        int byteLength = bytes.length;
        System.out.println("字符串的字节数为:" + byteLength);
    }
}
Copy after login

In the above sample code, we define a string variable str and initialize it for "Hello, world!". Then we use the str.getBytes() function to convert the string into a byte array and assign the result to the byte array variable bytes. Finally, we use bytes.length to get the length of the byte array and assign the result to the integer variable byteLength. Use the System.out.println() function again to output the number of bytes of the string to the console.

Run the above code, the output result is:

字符串的字节数为:14
Copy after login

As can be seen from the output result, the number of bytes of the string "Hello, world!" is 14, that is, the string consists of 14 composed of bytes.

Summary:
In Java programming, we can use the length() function of the String class to get the length of the string, that is, the number of characters in the string. If you need to get the number of bytes in a string, you can use the getBytes() function of the String class. When processing strings, choose the appropriate method to get the length or number of bytes according to your needs.

The above is the detailed content of Use java's String.length() function to get the length of a string. 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