Java length() method: get the length of the string
•
Java
length();
Example
String strCom2="C语言中文网"; int length=strCom2.length();
Typical application
Define the string object name and address, and assign values respectively. Connect the string name with address and assign it to the string message. Use the length method to output the length of these three strings. The code is as follows:
public static void main(String[]args){ String name="一棵山茶树";//定义字符串对象 String address="在一个遥远的小山村";//定义字符串对象 String message=address+"有"+name;//将字符串对象相连 System.out.println("字符串:"+name+"\t\t\t长度为:"+name.length());//输出字符串的长度 System.out.println("字符串:"+address+"\t\t长度为:"+address.length()); System.out.println("字符串:"+message+"\t长度为:"+message.length()); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码