首页 > 文档 > String 字符串
2017
07-19

String 字符串

Name

String 字符串

Examples

String str1 = “CCCP”;

char data[] = {‘C’, ‘C’, ‘C’, ‘P’};

String str2 = new String(data);

println(str1); // Prints “CCCP” to the console

println(str2); // Prints “CCCP” to the console

String   字符串 - 第1张  | Processing编程艺术

// Comparing String objects, see reference below.

String p = “potato”;

if (p == “potato”) {

println(“p == potato, yep.”); // This will not print

}

// The correct way to compare two Strings

if (p.equals(“potato”)) {

println(“Yes, the contents of p and potato are the same.”);

}

String   字符串 - 第2张  | Processing编程艺术

// Use a backslash to include quotes in a String

String quoted = “This one has \”quotes\””;

println(quoted); // This one has “quotes”

Description

A string is a sequence of characters. The class String includes methods for examining individual characters, comparing strings, searching strings, extracting parts of strings, and for converting an entire string uppercase and lowercase. Strings are always defined inside double quotes (“Abc”), and characters are always defined inside single quotes (‘A’).

To compare the contents of two Strings, use the equals() method, as in if (a.equals(b)), instead of if (a == b). A String is an Object, so comparing them with the == operator only compares whether both Strings are stored in the same memory location. Using the equals() method will ensure that the actual contents are compared. (The troubleshooting reference has a longer explanation.)

Because a String is defined between double quotation marks, to include such marks within the String itself you must use the \ (backslash) character. (See the third example above.) This is known as an escape sequence. Other escape sequences include \t for the tab character and \n for new line. Because backslash is the escape character, to include a single backslash within a String, you must use two consecutive backslashes, as in: \\

There are more string methods than those linked from this page. Additional documentation is located online in the official Java documentation.

字符串是字符的序列。类字符串包括检查单个字符、比较字符串、搜索字符串、提取字符串部分以及转换整个字符串大小写的方法。字符串总是在双引号 (“abc”) 内定义的, 字符总是在单引号 (“a”) 内定义的。

 

若要比较两个字符串的内容, 请使用 equals () 方法, (a. equals (b)), 而不是 if (a = = b)。字符串是一个对象, 因此将它们与 == 运算符进行比较只比较两个字符串是否存储在同一内存位置。使用 equals () 方法将确保比较实际的内容。(疑难解答参考有较长的解释。

 

由于字符串是在双引号之间定义的, 因此要在字符串本身中包含此类标记, 必须使用 \ (反斜线) 字符。(请参见上面的第三个示例。这称为转义序列。其他转义序列包括 \t 用于 tab 字符和 \n 用于新行。由于反斜线是转义字符, 若要在字符串内包含单个反斜线, 必须使用两个连续的斜杠, 如中所示: \

 

有更多的字符串方法比从这个网页链接。其他文档位于正式的 java 文档中。

Methods

charAt()

Returns the character at the specified index

 

返回指定索引处的字符

equals()

Compares a string to a specified object

将字符串与指定对象进行比较

indexOf()

Returns the index value of the first occurrence of a substring within the input string

返回输入字符串中子字符串的第一个匹配项的索引值

length()

Returns the number of characters in the input string返回输入字符串中的字符数

substring()

Returns a new string that is part of the input string返回一个新字符串, 它是输入字符串的一部分

toLowerCase()

Converts all the characters to lower case

将所有字符转换为小写

toUpperCase()

Converts all the characters to upper case

将所有字符转换为大写大小写

Constructor

String(data)

String(data, offset, length)

Parameters

data

byte[] or char[]: either an array of bytes to be decoded into characters, or an array of characters to be combined into a string

字节 [] char []: 要解码为字符的字节数组, 或要组合成字符串的字符数组

offset

int: index of the first character

int: 第一个字符的索引

length

int: number of characters

int: 字符数

Related

char
text()



最后编辑:
作者:卡萨布兰卡
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。