Java基本的程序设计结构(二)
来源:互联网 发布:俄罗斯女人外嫁知乎 编辑:程序博客网 时间:2024/04/25 16:33
(六)字符串
1.在这里需要指出的是它是一个对象,不是一个字符数组
2.他是不可变的,没有append()或reverse()
3.要用双引号,连接时用+
4.给出一些常用函数,相关的可查看API文档
toString();
int length() -- number of chars
char charAt(int index) -- char at given 0-based index
int indexOf(char c) -- first occurrence of char, or -1
int indexOf(String s)
boolean equals(Object) -- test if two strings have the same characters
boolean equalsIgnoreCase(Object) -- as above, but ignoring case
String toLowerCase() -- return a new String, lowercase
String substring(int begin, int end) -- return a new String made of the begin..end-1 substring from the original
5.初始化
s1 = new String(“This is some java String”);
s1 = new String(“This is some java String”);
6.例子
String a = "hello"; // allocate 2 String objects
String b = "there";
String c = a; // point to same String as a – fine
int len = a.length(); // 5
String d = a + " " + b; // "hello there"
int find = d.indexOf("there"); // find: 6
String sub = d.substring(6, 11); // extract: "there“
sub == b; // false (== compares pointers)
sub.equals(b); // true (a "deep" comparison)
String a = "hello"; // allocate 2 String objects
String b = "there";
String c = a; // point to same String as a – fine
int len = a.length(); // 5
String d = a + " " + b; // "hello there"
int find = d.indexOf("there"); // find: 6
String sub = d.substring(6, 11); // extract: "there“
sub == b; // false (== compares pointers)
Use the equals method;
s.equals(t) // returns true if the strings s and t are equal, false otherwise.
s and t can be string variables or string constants. "Hello".equals(command) // OK
To test if two strings are identical except for the upper/lowercase letter distinction
“Hello".equalsIgnoreCase("hello")
7.比较时一般不用==
String greeting = "Hello";
if (greeting == "Hello") . . . // probably true
if (greeting.substring(0, 4) == "Hell") . . . // probably false
8.StringBuffer
不同于String,
append() method- Lets you add characters to the end of a StringBuffer object
insert() method- Lets you add characters at a specified location within a StringBuffer object
setCharAt() method- Use to alter just one character in a StringBuffer
例子:
StringBuffer buff = new StringBuffer();
for (int i=0; i<100; i++) {
buff.append(<some thing>);
// efficient append
}
String result = buff.toString();
// make a String once done with appending
9.StringTokenizer
可以根据某些标记(token),来分割String
例子:
String testString = “one-two-three”;
StringTokenizer st = new StringTokenizer(testString,”-");
while (st.hasMoreTokens()) {
String nextName = st.nextToken();
System.out.println(nextName);
}
Result:
one
two
three
1.在这里需要指出的是它是一个对象,不是一个字符数组
2.他是不可变的,没有append()或reverse()
3.要用双引号,连接时用+
4.给出一些常用函数,相关的可查看API文档
toString();
int length() -- number of chars
char charAt(int index) -- char at given 0-based index
int indexOf(char c) -- first occurrence of char, or -1
int indexOf(String s)
boolean equals(Object) -- test if two strings have the same characters
boolean equalsIgnoreCase(Object) -- as above, but ignoring case
String toLowerCase() -- return a new String, lowercase
String substring(int begin, int end) -- return a new String made of the begin..end-1 substring from the original
5.初始化
s1 = new String(“This is some java String”);
s1 = new String(“This is some java String”);
6.例子
String a = "hello"; // allocate 2 String objects
String b = "there";
String c = a; // point to same String as a – fine
int len = a.length(); // 5
String d = a + " " + b; // "hello there"
int find = d.indexOf("there"); // find: 6
String sub = d.substring(6, 11); // extract: "there“
sub == b; // false (== compares pointers)
sub.equals(b); // true (a "deep" comparison)
String a = "hello"; // allocate 2 String objects
String b = "there";
String c = a; // point to same String as a – fine
int len = a.length(); // 5
String d = a + " " + b; // "hello there"
int find = d.indexOf("there"); // find: 6
String sub = d.substring(6, 11); // extract: "there“
sub == b; // false (== compares pointers)
Use the equals method;
s.equals(t) // returns true if the strings s and t are equal, false otherwise.
s and t can be string variables or string constants. "Hello".equals(command) // OK
To test if two strings are identical except for the upper/lowercase letter distinction
“Hello".equalsIgnoreCase("hello")
7.比较时一般不用==
String greeting = "Hello";
if (greeting == "Hello") . . . // probably true
if (greeting.substring(0, 4) == "Hell") . . . // probably false
8.StringBuffer
不同于String,
append() method- Lets you add characters to the end of a StringBuffer object
insert() method- Lets you add characters at a specified location within a StringBuffer object
setCharAt() method- Use to alter just one character in a StringBuffer
例子:
StringBuffer buff = new StringBuffer();
for (int i=0; i<100; i++) {
buff.append(<some thing>);
// efficient append
}
String result = buff.toString();
// make a String once done with appending
9.StringTokenizer
可以根据某些标记(token),来分割String
例子:
String testString = “one-two-three”;
StringTokenizer st = new StringTokenizer(testString,”-");
while (st.hasMoreTokens()) {
String nextName = st.nextToken();
System.out.println(nextName);
}
Result:
one
two
three
- Java基本的程序设计结构(二)
- Java基本的程序设计结构(一)
- Java基本的程序设计结构(三)
- Java基本的程序设计结构(四)
- Java基本的程序设计结构(五)
- java的基本程序设计结构(一)
- Java程序设计的基本结构
- java基本的程序设计结构
- Java的基本程序设计结构
- Java的基本程序设计结构
- Java 的基本程序设计结构
- Java基本的程序设计结构
- 《java核心技术》读书笔记(二)第三章 java基本的程序设计结构(1)
- Java核心技术(二) —— Java的基本程序设计结构
- 读书笔记(二)——《Java核心技术卷一》第三章Java的基本程序设计结构
- JAVA基本程序设计(二)
- Java基本程序设计结构
- 三、 Java基本的程序设计结构(一)
- strdup函数简介
- Lambda表达式
- 公关公司面试经历
- 读写某个设备上的寄存器的过程
- USB开发步骤之软件篇
- Java基本的程序设计结构(二)
- DHCP服务器配置---学习笔记
- 构建基于慢速网络的实时应用
- SRA 安全快速远程接入系统
- Dflying 陈黎夫谈《持续集成——软件质量改进和风险降低之道》
- 3.15晚会不够“狠” 央视让观众失望
- USB入门系列之一 —— USB概述
- 动态触发事件
- CentOS的yum加速