描述字符串的类——String类
一、String类
String类的特点:
字符串对象一旦被初始化就不会被改变
字符串定义的第一种方式——字符串常量池
字符串常量池中没有就建立,池中有,直接用


2.字符串中的操作方法
(1)构造方法
- String(byte[] bytes)
将一个字节数组变成字符串

- String(char[] value)
分配一个新的String,使其表示字符数组参数中当前包含的字符序列。

- String(char[] value,int offset,int count)
取自字符数组参数一个子数组的字符
也就是:从哪开始,取几个

(2)字符串功能
- 获取
a) 获取字符串中字符的个数(长度)
int length();
b) 根据位置获取字符
char charAt(int index);
c) 根据字符获取在字符串中的第一次出现的位置
int indexOf(int ch)
int indexOf(int ch,int fromIndex) //从指定位置进行ch的查找第一次出现的位置
int indexOf(String str) //索引字符串
int indexOf(String str,int fromIndex)
从后索引
int lastIndexOf(int ch)
int lastIndexOf(int ch,int fromIndex)
int lastIndexOf(String str)
int lastIndexOf(String str,int fromIndex)

d) 获取字符串中的一部分字符串(子串)
String substring(int beginIndex int endIndex)
该子字符串从指定的beginIndex处开始,直到索引endIndex-1处的字符。
String substring(int beginIndex)

- 转换
a) 将字符串变成字符串数组(字符串切割)
String[] split(String regex)
涉及到正则表达式

b) 将字符串变成数组
char[] toCharArray();

b) 将字符串转换成字节数组
byte[] getBytes();

c) 将字符串中的字母转成大小写
String toUpperCase(); //大写
String toLowerCase(); //小写

d) 将字符串中的内容进行替换
String replace(char oldChar,char newChar);

e) 将字符串两端的空白去除
String trim();

f) 将字符串进行连接
String concat(String str);

- 判断
a) 两个字符串内容是否相同
Boolean equals(Object obj);
忽略大小写比较字符串内容
Boolean equalsIgnoreCase(String str);

b)字符串中是否包含指定字符串
Boolean contains(String str);

c) 字符串是否以指定字符串开头,是否以指定字符串结尾
Boolean startsWith(string);
Boolean endsWith(string);

- 比较
按字典顺序比较两个字符串
int compareTo(String str);
int compareToIgnoreCase(String str);//不分大小写
相等,返回0;小于,返回小于0的值;大于,返回大于0的值。

- indexOf()方法
用于返回字符(串)在当前字符串中首次出现的索引位置。不存在返回-1
lastIndexOf()方法
用于从当前字符串的末尾开始向前查找首次出现的规定的字符(串)
吃几个大栗子
1.字符串数组排列
/*1.给定一个字符串数组,按照字典顺序进行从小到大的排列
{"nba","abc","vba","zz","qq","haha"}
*/
/*
* 思路:
* 1.对数组排序。可以用选择/冒泡。
* 2.for嵌套和比较及换位。
* 3.字符串对象比较。
* */
public class StringPractise {
public static void main(String[] args) {
String[] arr = { "nba", "abc", "vba", "zz", "qq", "haha" };
printArray(arr);
sortString(arr);
printArray(arr);
}
private static void sortString(String[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i].compareTo(arr[j]) > 0)// 字符串比较用compareTo
swap(arr, i, j);
}
}
}
private static void swap(String[] arr, int i, int j) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private static void printArray(String[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i != arr.length - 1)
System.out.print(arr[i] + ",");
else
System.out.println(arr[i] + "]");
}
}
}

2.子串的次数
/*一个子串在整串中出现的次数
* "nbaernbatynbauinbaopnba"
*
* 思路:
* 1.要找的子串是否存在,如果存在获取其出现的位置。用indexOf
* 2.找到了,记录出现的位置并在剩余的字符串中继续查找该子串,
* 而剩余字符串的起始位置是出现位置+子串的长度。
* 3.以此类推,通过循环完成查找,如果找不到就是-1,
* 并对i每次找到用计数器记录
* */
public class StringPractise2 {
public static void main(String[] args) {
String str = "nbaernbatynbauinbaopnba";
String key = "nba";
int count = getKeyStringCount(str, key);
System.out.println("count=" + count);
}
public static int getKeyStringCount(String str, String key) {
// 1.定义计数器
int count = 0;
// 2.定义变量,记录key出现的位置
int index = 0;
while ((index = str.indexOf(key)) != -1) {
str = str.substring(index + key.length());
count++;
}
return count;
}
}
第二种方法
public class Stringdemo {
public static void main(String[] main){
String str = "nbaernbatynbauinbaopnba";
String key = "nba";
int count = 0;
int index = 0;
while(index != -1){
index = str.indexOf(key,index+key.length());
count++;
}
System.out.println(count);
}
}
这个在数据结构中叫匹配模式!

3.最大相同的子串。
/*
* 3.两个字符串中最大相同的子串
* "qwerabcdtyuiop"
* "xabcdvbn"
*
* 思想:
* 1.取最大字符串,先看短的字符串是否在长的字符串中
* 如果存在,短的那个字符串就是最大字符串
* 2.如果不是,就将短的子串进行长度递减的方式去子串,再在长串中判断是否存在
* 如果存在就找到了!
* */
public class StringPractise3 {
public static void main(String[] args) {
String s1 = "qwerabcdtyuiop";
String s2 = "xabcdvbn";
String s = getMaxSubstring(s1, s2);
System.out.println("s=" + s);
}
public static String getMaxSubstring(String s1, String s2) {
for (int i = 0; i < s2.length(); i++) {
for (int a = 0, b = s2.length() - i; b != s2.length() + 1; a++, b++) {
String sub = s2.substring(a, b);
//System.out.println(sub);//打印得到字符串(见小栗子)
if(s1.contains(sub))
return sub;
}
}
return null;
}
}


4.去除两端空白
/*
* 4.模拟一个trim功能一致的方法.去除字符串两端的空白
*
* 思路
* 1.定义两个变量
* 一个作为从头开始判断字符串空格的角标。start++
* 一个作为从尾开始判断字符串空格的角标。end--
* 2.判断到不是空格为止,去头尾之间的字符串即可
* */
public class StringPractise4 {
public static void main(String[] args) {
String s = " ab c ";
s = myTrim(s);
System.out.println("-" + s + "-");
}
public static String myTrim(String s) {
int start = 0;
int end = s.length() - 1;
while (start <= end && s.charAt(start) == ' ') {
start++;
}
while (start <= end && s.charAt(end) == ' ') {
end--;
}
return s.substring(start, end + 1);
}
}

二、StringBuffer(字符串缓冲区)
StringBuffer:就是字符串缓冲区
用于存储数据的容器
-
特点
1.长度可变
2.可以存储不同类型的数据
3.最终要转成字符串进行使用
4.可以对字符串进行修改 -
StringBuffer是一个容器,所具备的功能:
增删改查 C(create)U(update)R(read)D(delete)
1.添加:
StringBuffer append(data)


StringBuffer insert(index,data)

2.删除:
StringBuffer delete(start,end)
包含头,不包含尾
StringBuffer deleteCharAt(int index)
删除指定位置的元素


3.查找:
char charAt(index);
int indexOf(string);
int lastIndexOf(string);
4.修改:
替换
StringBuffer replace(start,end,string);
void setCharAt(index,char);


- 其他方法
截取长度
StringBuffer setLength();

内容反转
StringBuffer reverse()

三、StringBuilder
StringBuilder和StringBuffer的功能一样,用法一样
不同的是
StringBuffer:是线程同步的。通常用于多线程
StringBuilder:是线程不同步的。通常用于单线程,提高效率
最后再给你个栗子吃
/*
* 将1个int数组变成字符串
* 用String做
* */
public class StringPractise {
public static void main(String[] args) {
int[] arr = { 3, 2, 1, 4, 8 };
String s = arrayToString(arr);
System.out.println(s);
}
public static String arrayToString(int[] arr) {
String str = "[";
for (int i = 0; i < arr.length; i++) {
if (i != arr.length - 1)
str += arr[i] + ",";
else
str += arr[i] + "]";
}
return str;
}
}
上述方法会产生无数个常量
/*
* 将1个int数组变成字符串
* 用StringBuilder来做
* */
public class StringBuilderPractise {
public static void main(String[] args) {
int[] arr = { 3, 2, 1, 4, 8 };
String s = arrayToString_2(arr);
System.out.println(s);
}
public static String arrayToString_2(int[] arr) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i != arr.length - 1)
sb.append(arr[i] + ",");
else
sb.append(arr[i] + "]");
}
return sb.toString();
}
}
此方法只会产生一个数组
因此建议用容器

网友评论