首页 > 编程知识 正文

string的length方法,数组有length方法吗

时间:2023-05-05 07:26:19 阅读:250830 作者:2453

Java String length() function returns the length of the sequence of characters represented by this object.

Java String length()函数返回此对象表示的字符序列的长度。

Java字符串长度 (Java String length)

Sometimes we have to get the length of string in java programs, below code snippet shows you how to do it.

有时我们必须获取Java程序中字符串的长度,下面的代码片段向您展示了如何做到这一点。

String str = "journaldev";System.out.println("String length is "+str.length());

Above snippet will produce output as:

上面的代码片段将产生如下输出:

String length is 10

Let’s say we have a function to print string length like below.

假设我们有一个函数来打印字符串长度,如下所示。

public static void printStringLength(String s){System.out.println("input string length is "+s.length());}

Can you guess what is the problem with above function? Well, it can lead to NullPointerException if someone is calling this function by passing string value as null. Below is a better implementation to avoid NullPointerException.

您能猜出上述功能有什么问题吗? 好吧,如果有人通过将字符串值传递为null来调用此函数,则可能导致NullPointerException 。 下面是避免NullPointerException的更好的实现。

public static void printStringLength(String s){if(s == null) return;System.out.println("input string length is "+s.length());}

Note that I am just ignoring and returning the call if input is null, you can log it or throw Exception or do anything else based on your requirement.

请注意,如果输入为null,我只是忽略并返回该调用,您可以记录它或引发Exception或根据您的要求执行任何其他操作。

Unicode表示形式的Java字符串长度 (Java String Length for Unicode representation)

If you look at the javadoc of String length() method, it says that the length is equal to the number of Unicode code units in the string. What does it mean? Let’s look at a small code snippet to easily understand meaning of this statement.

如果查看String length()方法的javadoc,它表示长度等于字符串中Unicode代码单元的数量。 这是什么意思? 让我们看一个小的代码片段,以轻松理解该语句的含义。

String str1 = "u00A9"; //copyright jxdwbl.out.println("String length is "+str1.length());

When you will run above program, string length will be printed as 1. In java, we can use Unicode characters to define a String and above unicode is for copyright character. Hence the length of String is 1 and if you will try to print it, it will be shown as ©.

当您在上面的程序中运行时,字符串长度将被打印为1。在Java中,我们可以使用Unicode字符定义一个String,而上面的unicode用于版权字符。 因此,字符串的长度为1,如果您尝试打印它,它将显示为© 。

Here is the final java string length program.

这是最终的Java字符串长度程序。

package com.journaldev.string;public class JavaStringLength {public static void main(String[] args) {String str = "journaldev";System.out.println("String length is " + str.length());String str1 = "u00A9";System.out.println("Unicode String length is " + str1.length());System.out.println("Unicode string value = "+str1);printStringLength(null);}public static void printStringLength(String s) {if (s == null)return; // do nothingSystem.out.println("input string length is " + s.length());}}

Below image shows the output produced by above program.

下图显示了以上程序产生的输出。

Java String长度,不使用length()函数 (Java String length without using length() function)

This is a very interesting interview question, there are many alternative and non-recommended ways to get length of string. Let’s just look at these, good for arguments but don’t use in production environment.

这是一个非常有趣的面试问题,有很多替代和不推荐的方法来获取字符串的长度。 让我们看一下这些参数,它们很适合作为参数,但不要在生产环境中使用。

Converting to character array and finding length

转换为字符数组并查找长度

public static int getStringLength(String s) {int count = 0;char [] ca = s.toCharArray();for (char c : ca) {count++;}return count;}

Calling length of character array

字符数组的调用长度

public static int getStringLength(String s) {return s.toCharArray().length;}

Using String lastIndexOf() function cleverly

巧妙地使用String lastIndexOf()函数

public static int getStringLength(String s) {return s.lastIndexOf("");}

I am sure there will be other funny ways to find string length without using length() function, I guess above are the easiest ones. That’s all for java string length example program.

我敢肯定还有其他有趣的方法可以找到不使用length()函数的字符串长度,我想上面是最简单的方法。 Java字符串长度示例程序就这些了。

References: API Doc, StackOverflow Article

参考: API Doc , StackOverflow文章

翻译自: https://www.journaldev.com/17817/java-string-length

win7电脑文件夹如何加密

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。