Processing的String类
官方参考链接:https://processing.org/reference/String.html
这里面的一些方法:
- examining individual characters 检查单个字符串
- comparing strings 比较字符串
- searching strings 搜索字符串
- extracting parts of strings 提取部分字符串
- converting an entire string uppercase and lowercase(转换整个字符串大写和小写)
字符串的定义
双引号引起来的"ABCD"
字符的定义
单引号引起来的’a’
比较字符串方法
equals()
if (a.equals(b))
//if (a == b) 是错误的方式 这种方式比较的是对象是否存储在相同的位置
这里a和b都是字符串对象
这里面也存在和C语言类似的规定是有关转义字符的。
\
\t
\n
\r
这些都是
测试例子1
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 字符串名字 来定义
- 字符可以通过 char 字符数组名[] 来定义(跟C语言类似)
- 字符数组可以转化成字符串输出 通过new的方式
测试例子2
// Comparing String objects, see reference below.
String p = "potato";
// The correct way to compare two Strings
print("p 的内容是 ");
println(p);
if (p.equals("potato")) {
println("p 的内容是potato");
}
if (p.equals("potat2o")) {
println(" p 的内容是popotat2o");
}
else
{
println("不同");
}
这个例子说明
print("p 的内容是 ");
print这个函数输出内容不带换行
println§;
println这个函数输出内容带换行
比较成功是真
比较失败是假
String quoted = "This one has \"quotes\"";
println(quoted); // This one has "quotes"
上面例子说明
原样输出“”需要用到转义字符\
构造函数(Constructors)
- String(data)
- String(data, offset, length)
Parameters参数描述
data - byte[] 字节数组(要解码成字符进而变成字符串)
- char[] 字符数组(字符数组组成字符串)
这个参数也很好理解,毕竟不管是字符还是字节在计算机存储看来都是一样的。
在C语言中一个字符也是占用一个字节。
offset
第一个字符串的索引
length
字符个数
方法(Methods)
- toUpperCase() Converts all of the characters in the string to
uppercase
把字符串的所有字符转换成大写 - toLowerCase() Converts all of the characters in the string to
lowercase
把字符串的所有字符转换成小写 - substring() Returns a new string that is a part of the original
string
返回一个新字符串这个字符串是原字符串的一部分(不太明白) - length() Returns the total number of characters included in the
String as aninteger number
返回一个字符串有多少个字符结果是一个整型数字 - indexOf() Returns the index value of the first occurrence of a
substring within the input string
返回输入字符串中第一次出现 asubstring 的索引值
可用于查找字符串位置 - equals() Compares two strings to see if they are the same
比较两个字符串以查看它们是否相同 - charAt() Returns the character at the specified index
返回指定索引处的字符
可用于引用字符串中的字符