Strings

本文深入解析 Java 中的 String 类,包括其基本概念、使用方法、内置方法、特殊支持及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The String class represents character strings. All string literals字面值 in Java programs, such as "abc", are implemented作为 as instances of实例 this class.
Strings are constant常量; their values cannot be changed after they are created. String bufferssupport缓冲 mutable可变的 strings. Because String objects are immutable不可变 they can beshared共享. For example:


     String str = "abc";
 
is equivalent to等效:


     char data[] = {'a', 'b', 'c'};
     String str = new String(data);
 
Here are some more examples of how strings can be used:


     System.out.println("abc");
     String cde = "cde";
     System.out.println("abc" + cde);
     String c = "abc".substring(2,3);
     String d = cde.substring(1, 2);
 
The class String includes methods for examining检查 individual单个 characters of the sequence, for comparing比较 strings, for searching搜索 strings, for extracting提取 substrings子字符串, and for creating a copy of a string字符串副本 with all characters translated转换 to uppercase or to lowercase. Case mapping大小写映射 is based on the Unicode Standard version标准版specified指定 by the Character class.

The Java language provides提供 special特别 support for the string concatenation串联 operator字符串 ( + ), and for conversion of转换 other objects to strings. String concatenation串联 isimplemented实现 through the StringBuilder(or StringBuffer) class and its append method. Stringconversions转换 are implemented实现 through the method toString, defined by由,决定 Object and inherited继承 by all classes in Java. For additional更多 information on stringconcatenation串联 and conversion转换, see Gosling, Joy, and Steele, The Java Language Specification.

Unless除非 otherwise noted另外说明, passing a null argument to a constructor构造函数 or method in this class will cause a NullPointerException to be thrown.

A String represents a string in the UTF-16 format in which supplementary characters增补字符are represented by surrogate代理 pairs (see the section Unicode Character Representations代表in the Character class for more information). Index values refer to指的是 char code units单元, so a supplementary增补 character uses two positions位置 in a String.

The String class provides提供 methods for dealing处理 with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values).

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值