Java 字符串常用方法

Java 字符串 length() 方法返回字符串的长度。 也是开发中经常用到的方法。

语法

字符串length()方法的语法如下:

public int length()

    参数

    返回值

    字符串长度。也可以说是字符的总数。

    方法示例

    介绍一些例子了解该方法的使用。

    方法示例1

    文件名: LengthExample.java

    public class LengthExample{
      public static void main(String args[]){
        String s1="yxjc123";
        String s2="python";
        System.out.println("string length is: "+s1.length());//7是yxjc123字符串的长度
        System.out.println("string length is: "+s2.length());//6 是python字符串的长度
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    输出:

    string length is: 7
    string length is: 6

    方法示例2

    由于 length() 方法给出了字符串中存在的字符总数;因此,也可以检查给定的字符串是否为空。

    文件名: LengthExample2.java

    public class LengthExample2 {
      public static void main(String[] args) {
        String str = "yxjc123";
        if(str.length()>0) {
          System.out.println("String is not empty and length is: "+str.length());
        }
        str = "";
        if(str.length()==0) {
          System.out.println("String is empty now: "+str.length());
        }
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    输出:

    String is not empty and length is: 7
    String is empty now: 0

    方法示例3

    length() 方法也可用于反转字符串。

    文件名: LengthExample3.java

    public class LengthExample3 
    {
      // 主要方法
      public static void main(String argvs[])
      {
        String str = "Welcome To yxjc123";
        int size = str.length();
        
        System.out.println("反转字符串: " + "'" + str + "'" + " is");
        
        for(int i = 0; i < size; i++)
        {
          // 以相反的顺序打印
          System.out.print(str.charAt(str.length() - i - 1));
        }
        
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    输出:

    反转字符串: 'Welcome To yxjc123' is
    321cjxy oT emocleW

    方法示例4

    length() 方法也可用于查找字符串中存在的空格。请看以下示例。

    文件名: LengthExample4.java

    public class LengthExample4
    {
      // 主方法
      public static void main(String argvs[])
      {
        String str = " Welcome To yxjc123 ";
        int sizeWithWhiteSpaces = str.length();
        
        System.out.println("In the string: " + "'" + str + "'");
        
        str = str.replace(" """);
        int sizeWithoutWhiteSpaces = str.length();
        
        // 计算空白
        int noOfWhieSpaces = sizeWithWhiteSpaces - sizeWithoutWhiteSpaces;
        
        System.out.print("Total number of whitespaces present are: " + noOfWhieSpaces);
      }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    输出:

    In the string: ' Welcome To yxjc123 '
    Total number of whitespaces present are: 4

    内部实现

    public int length() {
      return value.length;
    }
    • 1
    • 2

    String 类在内部使用 char[] 数组来存储字符。数组的长度变量用于查找数组中存在的元素总数。由于 Java String 类在内部使用了这个 char[] 数组;因此,长度变量不能暴露给外界。因此,Java 开发人员创建了 length() 方法,公开了长度变量的值。也可以将 length() 方法视为 getter() 方法,它为用户提供类字段的值。内部实现清楚地描述了 length() 方法返回的是长度变量的值。