Core Java Volume I — 3.6. Strings

3.6. Strings
Conceptually, Java strings are sequences of Unicode characters(Java的字符串是一个Unicode序列). For example, the string "Java\u2122" consists of the five Unicode characters J, a, v, a, and ?. Java does not have a built-in string type(Java没有内置的字符串类型). Instead, the standard Java library contains a predefined class called, naturally enough, String. Each quoted string is an instance of the String class:

String e = ""; // an empty string
String greeting = "Hello";

3.6.1. Substrings
You can extract a substring(提取子串) from a larger string with the substring method(substring方法) of the String class. For example,

String greeting = "Hello";
String s = greeting.substring(0, 3);

creates a string consisting of the characters "Hel".
The second parameter of substring is the first position that you do not want to copy. In our case, we want to copy positions 0, 1, and 2 (from position 0 to position 2 inclusive). As substring counts it, this means from position 0 inclusive to position 3 exclusive(起始位置包括第1个参数,结束位置不包含第2个参数).
There is one advantage to the way substring works: Computing the length of the substring is easy. The string s.substring(a, b) always has length b-a. For example, the substring "Hel" has length 3–0=3.
3.6.2. Concatenation(串联/连结)
Java, like most programming languages, allows you to use + to join (concatenate) two strings(使用+号连结连个字符串).

String expletive = "Expletive";
String PG13 = "deleted";
String message = expletive + PG13;

The preceding code sets the variable message to the string "Expletivedeleted". (Note the lack of a space between the words: The + operator joins two strings in the order received, exactly as they are given.)
When you concatenate a string with a value that is not a string, the latter is converted to a string(字符串与非字符串连结时,非字符串字面直接转为字符串). (As you will see in Chapter 5, every Java object can be converted to a string.) For example,

int age = 13;
String rating = "PG" + age;

sets rating to the string "PG13".
This feature is commonly used in output statements. For example, System.out.println("The answer is " + answer); is perfectly acceptable and prints what you would expect (and with the correct spacing because of the space after the word is).
3.6.3. Strings Are Immutable(字符串是不可变的)
The String class gives no methods that let you change a character in an existing string(String类没有可以改变字符串值的方法). If you want to turn greeting into "Help!", you cannot directly change the last positions of greeting into 'p' and '!'. If you are a C programmer, this will make you feel pretty helpless.
How are we going to modify the string? In Java, it is quite easy: Concatenate the substring that you want to keep with the characters that you want to replace.

greeting = greeting.substring(0, 3) + "p!";

This declaration changes the current value of the greeting variable to "Help!".
Since you cannot change the individual characters in a Java string, the documentation refers to the objects of the String class as immutable. Just as the number 3 is always 3, the string "Hello" will always contain the code-unit sequence for the characters H, e, l, l, o. You cannot change these values. Yet you can, as you just saw, change the contents of the string variable greeting and make it refer to a different string, just as you can make a numeric variable currently holding the value 3 hold the value 4.
Isn't that a lot less efficient? It would seem simpler to change the code units than to build up a whole new string from scratch. Well, yes and no. Indeed, it isn't efficient to generate a new string that holds the concatenation of "Hel" and "p!". But immutable strings have one great advantage: The compiler can arrange that strings are shared.
To understand how this works, think of the various strings as sitting in a common pool.
String variables then point to locations in the pool. If you copy a string variable, both the original and the copy share the same characters.
Overall, the designers of Java decided that the efficiency of sharing outweighs the inefficiency of string editing by extracting substrings and concatenating. Look at your own programs; we suspect that most of the time, you don't change strings—you just compare them. (There is one common exception—assembling strings from individual characters or from shorter strings that come from the keyboard or a file. For these situations, Java provides a separate class that we describe in Section 3.6.8, "Building Strings," on p. 74.)


C++ Note
C programmers are generally bewildered when they see Java strings for the first time because they think of strings as arrays of characters:

char greeting[] = "Hello";

That is a wrong analogy: A Java string is roughly analogous to a char* pointer,

char* greeting = "Hello";

When you replace greeting with another string, the Java code does roughly the following:

char* temp = malloc(6);
strncpy(temp, greeting, 3);
strncpy(temp + 3, "p!", 3);
greeting = temp;

Sure, now greeting points to the string "Help!". And even the most hardened C programmer must admit that the Java syntax is more pleasant than a sequence of strncpy calls. But what if we make another assignment to greeting?

greeting = "Howdy";

Don't we have a memory leak(内存泄露)? After all, the original string was allocated on the heap.
Fortunately, Java does automatic garbage collection. If a block of memory is no longer needed, it will eventually be recycled.
If you are a C++ programmer and use the string class defined by ANSI C++, you will be much more comfortable with the Java String type. C++ string objects also perform automatic allocation and deallocation of memory. The memory management is performed explicitly by constructors, assignment operators, and destructors.
However, C++ strings are mutable(C++中的string是可变的)—you can modify individual characters in a string.


3.6.4. Testing Strings for Equality
To test whether two strings are equal, use the equals method. The expression

s.equals(t)

returns true if the strings s and t are equal, false otherwise. Note that s and t can be string variables or string constants. For example, the expression

"Hello".equals(greeting)

is perfectly legal(完全合法). To test whether two strings are identical except for the upper/lowercase letter distinction, use the equalsIgnoreCase method.

"Hello".equalsIgnoreCase("hello")

Do not use the == operator to test whether two strings are equal(不要使用==操作法来比较两个字符串是否相等)! It only determines whether or not the strings are stored in the same location(它只表明两个字符串是否存储在同一个位置). Sure, if strings are in the same location, they must be equal. But it is entirely possible to store multiple copies of identical strings in different places.

String greeting = "Hello"; //initialize greeting to a string
if (greeting == "Hello") . . .// probably true
if (greeting.substring(0, 3) == "Hel") . . .// probably false

If the virtual machine always arranges for equal strings to be shared, then you could use the == operator for testing equality. But only string constants are shared, not strings that are the result of operations like + or substring. Therefore, never use == to compare strings lest you end up with a program with the worst kind of bug —— an intermittent one that seems to occur randomly.


C++ Note
If you are used to the C++ string class, you have to be particularly careful about equality testing. The C++ string class does overload the == operator to test for equality of the string contents. It is perhaps unfortunate that Java goes out of its way to give strings the same "look and feel" as numeric values but then makes strings behave like pointers for equality testing. The language designers could have redefined == for strings, just as they made a special arrangement for +. Oh well, every language has its share of inconsistencies.
C programmers never use == to compare strings but use strcmp instead. The Java method compareTo is the exact analog to strcmp. You can use

if (greeting.compareTo("Hello") == 0) . . .

but it seems clearer to use equals instead.


3.6.5. Empty and Null Strings
The empty string "" is a string of length 0. You can test whether a string is empty by calling

if (str.length() == 0)

or

if (str.equals(""))

An empty string(空串) is a Java object which holds the string length (namely 0) and an empty contents. However, a String variable can also hold a special value, called null, that indicates that no object is currently associated with the variable. (See Chapter 4 for more information about null.) To test whether a string is null, use the condition

if (str == null)

Sometimes, you need to test that a string is neither null nor empty. Then use the condition

if (str != null && str.length() != 0)

You need to test that str is not null first. As you will see in Chapter 4, it is an error to invoke a method on a null value.
3.6.5. Code Points and Code Units
Java strings are implemented as sequences of char values. As we discussed in Section 3.3.3, "The char Type," on p.49, the char data type is a code unit for representing Unicode code points in the UTF-16 encoding. The most commonly used Unicode characters can be represented with a single code unit(最常用的Unicode字符可以用一个代码单元来表示). The supplementary characters require a pair of code units(补充字符需要一双代码单元).
The length method yields the number of code units required for a given string in the UTF-16 encoding. For example:

String greeting = "Hello";
int n = greeting.length(); // is 5.

To get the true length — that is, the number of code points—call

int cpCount = greeting.codePointCount(0, greeting.length());

The call s.charAt(n) returns the code unit at position n, where n is between 0 and s.length()–1. For example:

char first = greeting.charAt(0); // first is 'H'
char last = greeting.charAt(4); // last is 'o'

To get at the ith code point, use the statements

int index = greeting.offsetByCodePoints(0, i);
int cp = greeting.codePointAt(index);

 


Note
Like C and C++, Java counts code units and code points in strings starting with 0.


Why are we making a fuss about code units? Consider the sentence

Φ is the set of integers
The character Φ requires two code units in the UTF-16 encoding. Calling

char ch = sentence.charAt(1)

doesn't return a space but the second code unit of Φ. To avoid this problem, you should not use the char type. It is too low-level.
If your code traverses a string, and you want to look at each code point in turn, use these statements:

int cp = sentence.codePointAt(i);
if (Character.isSupplementaryCodePoint(cp)) i += 2;
elsei++;

You can move backwards with the following statements:

i--;
if (Character.isSurrogate(sentence.charAt(i))) i--;
int cp = sentence.codePointAt(i);

3.6.6. The String API
The String class in Java contains more than 50 methods. A surprisingly large number of them are sufficiently useful so that we can imagine using them frequently. The following API note summarizes the ones we found most useful.


Note
These API notes, found throughout the book, will help you understand the Java Application Programming Interface (API). Each API note starts with the name of a class such as java.lang.String (the significance of the so-called package name java.lang is explained in Chapter 4). The class name is followed by the names, explanations, and parameter descriptions of one or more methods.
We typically do not list all methods of a particular class but select those that are most commonly used and describe them in a concise form. For a full listing, consult the online documentation (see Section 3.6.7, "Reading the Online API Documentation," on p. 72).
We also list the version number in which a particular class was introduced. If a method has been added later, it has a separate version number.


java.lang.String 1.0

  • char charAt(int index)returns the code unit at the specified location. You probably don't want to call this method unless you are interested in low-level code units.
  • int codePointAt(int index) 5.0:returns the code point that starts or ends at the specified location.
  • int offsetByCodePoints(int startIndex, int cpCount) 5.0:returns the index of the code point that is cpCount code points away from the code point at startIndex.
  • int compareTo(String other)returns a negative value if the string comes before other in dictionary order, a positive value if the string comes after other in dictionary order, or 0 if the strings are equal.
  • boolean endsWith(String suffix)returns true if the string ends with suffix.
  • boolean equals(Object other)returns true if the string equals other.
  • boolean equalsIgnoreCase(String other)returns true if the string equals other, except for upper/lowercase distinction.
  • int indexOf(String str)
  • int indexOf(String str, int fromIndex)
  • int indexOf(int cp)
  • int indexOf(int cp, int fromIndex)returns the start of the first substring equal to the string str or the code point cp, starting at index 0 or at fromIndex, or -1 if str does not occur in this string.
  • int lastIndexOf(String str)
  • int lastIndexOf(String str, int fromIndex)
  • int lastindexOf(int cp)
  • int lastindexOf(int cp, int fromIndex)returns the start of the last substring equal to the string str or the code point cp, starting at the end of the string or at fromIndex.
  • int length()returns the length of the string.
  • int codePointCount(int startIndex, int endIndex) 5.0:returns the number of code points between startIndex and endIndex - 1. Unpaired surrogates are counted as code points.
  • String replace(CharSequence oldString, CharSequence newString)returns a new string that is obtained by replacing all substrings matching oldString in the string with the string newString. You can supply String or StringBuilder objects for the CharSequence parameters.
  • boolean startsWith(String prefix)returns true if the string begins with prefix.
  • String substring(int beginIndex)
  • String substring(int beginIndex, int endIndex)returns a new string consisting of all code units from beginIndex until the end of the string or until endIndex - 1.
  • String toLowerCase()returns a new string containing all characters in the original string, with uppercase characters converted to lowercase.
  • String toUpperCase()returns a new string containing all characters in the original string, with lowercase characters converted to uppercase.
  • String trim()returns a new string by eliminating all leading and trailing spaces in the original string.

3.6.8. Building Strings
Occasionally, you need to build up strings from shorter strings, such as keystrokes or words from a file. It would be inefficient to use string concatenation for this purpose. Every time you concatenate strings, a new String object is constructed. This is time-consuming and wastes memory. Using the StringBuilder class avoids this problem.
Follow these steps if you need to build a string from many small pieces. First, construct an empty string builder:

StringBuilder builder = new StringBuilder();

(We discuss constructors and the new operator in detail in Chapter 4.)
Each time you need to add another part, call the append method.

builder.append(ch); // appends a single character
builder.append(str); // appends a string

When you are done building the string, call the toString method. You will get a String object with the character sequence contained in the builder.

String completedString = builder.toString();

 


Note
The StringBuilder class was introduced in JDK 5.0. Its predecessor, StringBuffer, is slightly less efficient, but it allows multiple threads to add or remove characters. If all string editing happens in a single thread (which is usually the case), you should use StringBuilder instead. The APIs of both classes are identical.


The following API notes contain the most important methods for the StringBuilder class.
java.lang.StringBuilder 5.0

  • StringBuilder()constructs an empty string builder.
  • int length()returns the number of code units of the builder or buffer.
  • StringBuilder append(String str)appends a string and returns this.
  • StringBuilder append(char c)appends a code unit and returns this.
  • StringBuilder appendCodePoint(int cp)appends a code point, converting it into one or two code units, and returns this.
  • void setCharAt(int i, char c)sets the ith code unit to c.
  • StringBuilder insert(int offset, String str)inserts a string at position offset and returns this.
  • StringBuilder insert(int offset, char c)inserts a code unit at position offset and returns this.
  • StringBuilder delete(int startIndex, int endIndex)deletes the code units with offsets startIndex to endIndex - 1 and returns this.
  • String toString()returns a string with the same data as the builder or buffer contents.

 

转载于:https://www.cnblogs.com/utank/p/4424784.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/257431.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android实用代码七段(五)

前言 每次分享意味着每次都有进步,本系列以实用为主,欢迎和我分享和推荐好用的代码段~~声明欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com农民伯伯: http://over140.cnblogs.com 正文 1、展开、收起…

oracle 自增1,oracle自增无法从1开始

问题描述我想让XH字段从1开始增加,由于是varchar类型的,所以就用这种方式,但我发现我的数据表中XH字段是从217开始增加的,为什么啊问题出现的环境背景及自己尝试过哪些方法相关代码// 请把代码文本粘贴到下方(请勿用图片代替代码)declarej number;i number;begini:1;j:1;for i …

ceph Luminous版手动安装零散记录

1.安装必要的依赖包,关防火墙,向/etc/hosts内添加域名等 2.安装ceph 配置yum源 (如果嫌慢,可以配置cachedir/home/yum/$basearch/$releasever和keepcache1两个参数,在第一次安装时将安装包下载到本地做成yum源,给后面的…

C#最简单最完整的webservice实例

我做java,但最近接触crm所以必须研究一下C#中的webservice以备后用,其实就是个新手,哈哈,这个实例是我在参考了网上诸多不完整的例子的情况下,自己摸索完成的。期间遇到过一系列的棘手的问题,经过个人努力终…

2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈

秋实大哥去打工 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59Description 天行健,君子以自强不息。地势坤,君子以厚德载物。天天过节的秋实大哥又要过节了,于是他要给心爱的妹子买礼物。但由…

oracle怎么通过sid确定表名,如何获取Oracle的SID列表

更好的方法是,如果您有权访问主机并且Oracle安装使用以下命令:lsnrctl status。这适用于Unix,Linux和Windows机器。 status命令将显示所有监听器(及其相关的SID)。C:\>lsnrctl statusLSNRCTL for 32-bit Windows: Version 10.2.0.1.0 - Pr…

51 Nod 1007 正整数分组【类01背包】

1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10难度:2级算法题将一堆正整数分为2组,要求2组的和相差最小。例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1…

YTU 2924: 文件操作--二进制文件读入

2924: 文件操作--二进制文件读入 时间限制: 1 Sec 内存限制: 128 MB提交: 58 解决: 20题目描述 现有100名学生的姓名(name)、学号(num)、英语(English)、数学(Math)、语文(Chinese)成绩存储在一个二进制文件student.dic中(姓名用char[20],学号和各科成绩用int存储…

oracle 9.2.0.4,CentOS 4.7 安装Oracle 9.2.0.4的一些问题

#vi/etc/sysconfig/iptables,增加如下-A INPUT -p udp -s 0/0 -d 0/0 --dport 177 -j ACCEPT-A INPUT -p tcp -s 0/0 -d 0/0 --dport telnet -j ACCEPT-A INPUT -p tcp -s 0/0 -d 0/0 --dport ssh -j ACCEPT-A INPUT -p tcp -s 0/0 -d 0/0 --dport login -j ACCEPT-…

《机电传动控制》----学习笔记六

《机电传动控制》与其他学科的联系 1、《液压传动与气压传动》中提到的液压控制阀中的电液伺服阀与《机电传动控制》中的控制电动机里的伺服电机有着密切的联系,都要求我们对伺服系统有着很好的理解。 2、《电路理论》中电机作为独立的一章,讲到了用向量…

Oracle Imp and Exp (导入和导出) 数据 工具使用

Oracle 提供两个工具imp.exe 和exp.exe分别用于导入和导出数据。这两个工具位于Oracle_home/bin目录下。 导入数据exp 1 将数据库ATSTestDB完全导出,用户名system 密码123456 导出到c:\export.dmp中 exp system/123456ATSTestDB filec:\export.dmp fully 其中ATSTestDB为数据库…

[Oracle][Corruption]究竟哪些检查影响到 V$DATABASE_BLOCK_CORRUPTION

根据 471716.1,11g 之后,下列动作如果遇到坏块,都会输出记录到 V$DATABASE_BLOCK_CORRUPTION。- Analyze table .. Validate structure- CTAS(Create table as Select)- Export另外,这些也会记录的:RMAN > Vali…

oracle使用loop将增加十天,使用loop循环操作DML语句

---loop循环:--创建测试表:suxingPROD>create table total3(2 t1 number(8),3 t2 number(8),4 cr date default sysdate);Table created.#测试表已经创建。--查看表中原来的数据:suxingPROD>select * from total3;T1 T2 CR-…

iOS富文本

iOS富文本 背景:前些天突然想做一个笔记本功能,一开始,觉得挺简单的呀,一个UITextView,网络缓存也不干了,直接本地NSUserDefault存储,然后完事了,美工,弄几张好看的图片,…

SQL编程题-----1

首先,题目给出这个数据库表格 要求写出SQL语句使之变成如下表格 解决方法: SELECT t1.Rq,t1.胜,t2.负 FROM //t1和t2是自己命的新表格的名字 (SELECT Rq,COUNT(*) AS 胜 //As 胜意思是输出结果时列名为”胜“FROM testtableWHERE Sh…

maven设置jdk版本

两种方式&#xff1a;一、可以修改 MAVEN 的 setting.xml 文件&#xff0c;统一修改。<profiles> <profile> <id>jdk-1.6</id> <activation> <activeByDefault>true</activeByDefault>…

获取系统时间出错oracle-,oracle 获取系统时间(转)

Oracle中如何获取系统当前时间select to_char(sysdate,yyyy-mm-dd hh24:mi:ss) from dual;ORACLE里获取一个时间的年、季、月、周、日的函数select to_char(sysdate, yyyy ) from dual; --年select to_char(sysdate, MM ) from dual; --月select to_char(sysdate, dd ) f…

PHP环境搭建

以Apache模块运行PHP环境搭建方法 下载Apache 注意&#xff1a;在http://www.apachelounge.com/ 下载Apache&#xff0c;因为该网站提供的Apache是通过更高版本的VC编译器编译的。由于接下来我下载的PHP版本是VC11的&#xff0c;所以下载的Apache版本也是基于VC11的。 download…

Java语言中的-----访问修饰符

day04 Java语言中的----访问修饰符一、访问修饰符概述&#xff1a;访问修饰符就是对变量或者是方法或者是类的一个修饰&#xff0c;通过修饰以后实现一些必要的权限&#xff0c;主要是说明类成员如何被使用的作用。二、访问修饰符1、访问修饰符有哪些&#xff1f;访问修饰符总共…

六角填数---第五届蓝桥杯

/** 如图【1.png】所看到的六角形中&#xff0c;填入1~12的数字。使得每条直线上的数字之和都同样。图中&#xff0c;已经替你填好了3个数字&#xff0c;请你计算星号位置所代表的数字是多少&#xff1f;请通过浏览器提交答案。不要填写多余的内容。*/ public class 六角填数 {…