---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
String类用于描述字符串事物的一个类,
★特点:String类对象内容一旦被初始化,就不能再改变。
1
2
3
4
5
6
7
8
|
String s =
new
String();
//等同于String s = "";
String s1 =
"abc"
;
//String是类,因此"abc"是一个String的实例对象
s1 =
"a"
;
//s1引用指向改变从指向"abc"改变到指向对象"a"
String s2 =
new
String(
"abc"
);
//s2是两个对象(new产生一个对象、"abc"对象)
s1==s2;
//在此比地址
s1.equals(s2);
//比内容
s1和s2区别?
s1代表一个对象,s2有两个对象
|
String类的常见方法:获取、判断
★获取★
1、字符串长度:int length()
2、指定位字符:char charAt(int index)
3、字符位置:int indexOf(int char) 返回char在字符串中的第一次出现位置,如果没有找到return -1
int indexOf(int char,int fromIndex) 从指定位开始查找
int indexOf(int String)
int indexOf(int String,int fromIndex) 从指定位开始查找
代码实例:
1
2
3
4
5
6
7
8
9
10
|
public
class
StringDemo {
public
static
void
main(String args[]) {
String s =
"vubjhbj"
;
System.out.println(s.length());
System.out.println(s.charAt(
3
));
System.out.println(s.indexOf(
'a'
));
//查找不到return -1
System.out.println(s.indexOf(
"bj"
,
3
));
}
}
|
★判断★
1、是否包含某字符:boolean contains(str)
int indexOf(str) //用于既要判断又要查具体位置
2、是否有内容:boolean isEmpty() //原理str.length() ?= 0
3、开头结尾是不是某一字符:startsWith(str)
endsWith(str)
4、内容异同:boolean equals(str)
boolean equalsIgnoreCase(str) //忽略大小写的比较
关于判断的代码实例:
1
2
3
4
5
6
7
8
9
10
|
public
class
StringDemo {
public
static
void
main(String args[]) {
String s =
"StringDemo.java"
;
System.out.println(s.contains(
"String"
));
System.out.println(s.isEmpty());
System.out.println(s.startsWith(
"String"
));
System.out.println(s.equals(
"stringDemo.java"
));
System.out.println(s.equalsIgnoreCase(
"stringdemo.java"
));
}
}
|
★转化★
1、字符(/节)数组转化为字符串:String(char[]) //char[]--->byte[] ???
String(char[],offset,count)
static String copyValueOf(char[])
static String valueOf(char[])
2、字符串转化为数组:toCharArray()
3、基本数据类型转化为字符串:static Sting valueOf(int) //int--->其他基本数据类型
字符与数组间的转化实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public
class
StringDemo {
public
static
void
main(String args[]) {
// 基本数据类型--->字符串????
int
a =
50
;
String s3 =
50
+
""
;
// String s3 = String.valueOf(500);
System.out.println(s3);
// 字符串--->字符数组
String s2 =
"sfgbgfbg"
;
char
[] c2 = s2.toCharArray();
for
(
int
i=
0
;i<c2.length;i++) {
System.out.print(c2[i]+
"-"
);
}
// 字符数组--->字符串
char
[] c1 = {
's'
,
'h'
,
'f'
};
String s1 = String.valueOf(c1,
0
,
3
);
// String s1 = String.copyValueOf(c1,0,2);
// String s1 = new String(c1,1,c1.length-1); //构造方法
System.out.println(s1);
}
}
|
★替换★
String replace(oldChar,newChar) //通过new char实现替换
String replace(charSequence target,charSequence replacement) //头--->尾???
----->字符串替换(charSequence目标,charSequence替换)
1
2
3
4
5
6
7
8
|
public
class
ReplaceDemo {
public
static
void
main(String args[]) {
String s =
"ReplaceDemo.java"
;
// String t = s.replace(CharSequence target,CharSequence replacement);????
String t = s.replace(
"java"
,
"txt"
);
System.out.println(t);
}
}
|
★切割★
String[ ] split(regex)
获取字符串中的一部分:String substring(begin)
String substring(begin,end)
★转换、去空、比较★
字符串转换为小写:String toLowerCase()
字符串转换为大写:String toUpperCase()
字符串不分大小写的比较:int compareTo(String) ???
------------>在数字上比较两个Byte 对象。两个对象Byte值相同 return 0,不同 return
去除字符串两端空格:String trim()
转换、去空实例:
1
2
3
4
5
6
7
8
9
10
11
|
public
class
TrimDemo {
public
static
void
main(String args[]) {
String s =
" Trim Demo.java "
;
String r = s.trim();
System.out.println(r);
// String t = s.toLowerCase();
// String r = s.toUpperCase();
// System.out.println(t+"---"+r);
}
}
|
字符串--->基本数据
String s = "123";
int a = new Integer(s);
获取一段字符串中含有某一子字符串的个数的方法定义:
思路:
1、定义一个技术器(用于记录所寻找子串的个数)。
2、判断。
3、返回寻找到的个数。
初步代码实现:
1
2
3
4
5
6
7
8
|
int
myContains(String str,String ch) {
//1、定义计数
int
count;
//2、寻找
for
(;;) {}
//3、返回个数
return
count;
}
|
代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public
class
ContainsDemo {
public
static
void
main(String args[]) {
String s =
" hjghjgfusd vhj "
;
String ch =
"hj"
;
System.out.print(ch+
"出现的次数:"
);
int
x = myContains(s, ch);
System.out.println(x);
}
public
static
int
myContains(String s,String ch) {
//定义计数
int
count =
0
;
//判断出现次数
for
(
int
i=
0
;i<s.length()-
1
;i++) {
if
(s.indexOf(ch,i) >=
0
) {
count++;
i = s.indexOf(ch,i)+ch.length()-
1
;
}
}
//返回次数
return
count ;
}
}
|
---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------