博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java String length()方法
阅读量:2533 次
发布时间:2019-05-11

本文共 3919 字,大约阅读时间需要 13 分钟。

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 if someone is calling this function by passing string value as null. Below is a better implementation to avoid NullPointerException.

您能猜出上述功能有什么问题吗? 好吧,如果有人通过将字符串值传递为null来调用此函数,则可能导致 。 下面是避免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 character		System.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 nothing		System.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 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: ,

参考: ,

翻译自:

转载地址:http://vumzd.baihongyu.com/

你可能感兴趣的文章
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>