这一块的主要内容是有关Java中有关String类以及StringBuffer类的内容
String类
String类的转换功能
- byte[] getBytes()
- char[] toCharArray()
- static String valueOf(char[] chs)
- static String valueOf(int i)
- String toLowerCase()
- String toUpperCase()
- String concat(String str)
代码举例:
public class StringDemo10 {
public static void main(String[] args) {
String s = "BigData";
byte[] bytes = s.getBytes();
System.out.println(bytes);
for(int i=0;i<bytes.length;i++){
System.out.println(bytes[i]);
}
System.out.println("****************************************");
char[] chars = s.toCharArray();
System.out.println(chars);
for(int i=0;i<chars.length;i++){
System.out.println(chars[i]);
}
System.out.println("****************************************");
String s1 = String.valueOf(chars);
System.out.println(s1);
System.out.println("****************************************");
String s2 = String.valueOf(100);
System.out.println(s2);
System.out.println("****************************************");
String s3 = s.toLowerCase();
System.out.println(s3);
System.out.println("****************************************");
String s4 = s.toUpperCase();
System.out.println(s4);
System.out.println("****************************************");
String s5 = "hello";
String s6 = "world";
String s7 = s5 + s6;
String s8 = s5.concat(s6);
System.out.println("s5:"+s5);
System.out.println("s6:"+s6);
System.out.println("s7:"+s7);
System.out.println("s8:"+s8);
}
}
- 小练习:将一个字符串的首字母转换成大写,其余转换成小写
举例:helloWORld->Helloworld 代码:
public static void main(String[] args) {
String s = "helloWorld";
String c = s.substring(0,1);
String s1 = s.substring(1);
String c1 = c.toUpperCase();
String s2 = s1.toLowerCase();
System.out.println(c1.concat(s2));
System.out.println(s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase()));
String s4 = "helloWORld";
System.out.println(s4.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase()));
}
String类的其他功能
替换功能
- String replace(char old,char new)
- String replace(String old,String new)
String s = "HelloWorld";
String replace = s.replace("l", "m");
System.out.println(replace);
String replace1 = s.replace("llo", "ppl");
System.out.println(replace1);
去除字符串两空格
String s1 = " HelloWorld ";
String trim = s1.trim();
System.out.println(trim);
按字典顺序比较两个字符串
- int compareTo(String str)
- int compareToIgnoreCase(String str)
```c
String s5 = "hello";
String s6 = "hello";
String s7 = "abc";
String s8 = "qwe";
String s9 = "hel";
System.out.println(s5.compareTo(s6));
System.out.println(s5.compareTo(s7));
System.out.println(s5.compareTo(s8));
System.out.println(s5.compareTo(s9));
String类练习
把数组中的数据按照指定格式拼接成一个字符串
举例:int[] arr = {1,2,3}; 输出:[1,2,3] 代码:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[3];
for (int i=0;i<arr.length;i++){
System.out.println("请输入第"+(i+1)+"个数字");
int num = sc.nextInt();
arr[i] = num;
}
String s = arrayToString(arr);
System.out.println(s);
}
public static String arrayToString(int[] arr){
String s ="";
s+="[";
for(int i=0;i<arr.length;i++){
if(i == arr.length-1){
s+=arr[i];
s+="]";
}else {
s+=arr[i];
s+=",";
}
}
return s;
}
字符串反转
举例:键盘录入“abc” 输出结果:“cba”
public static void main(String[] args) {
System.out.println("请输入一串字符");
Scanner sc = new Scanner(System.in);
String s = sc.next();
String over_s = "";
char[] chars = s.toCharArray();
for(int i=chars.length-1;i>=0;i--){
over_s += chars[i];
}
System.out.println("反转之后的字符串");
System.out.println(over_s);
}
统计大串中小串出现的次数
举例: 在字符串“woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagune”中Java出现了五次
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入大串");
String maxString = sc.next();
System.out.println("请输入小串");
String minString = sc.next();
int result = findString(maxString,minString);
System.out.println("大串中小串出现的次数为:"+result);
}
public static int findString(String maxString,String minString){
int count = 0;
int index = maxString.indexOf(minString);
if(index == -1){
System.out.println("大串中没有这个小串");
return 0;
}
while (index != -1){
count ++;
int startindex = index + minString.length();
maxString = maxString.substring(startindex);
index = maxString.indexOf(minString);
}
return count;
}
StringBuffer类
- StringBuffer:
- 线程安全,可变的字符序列。字符串缓冲区就像一个String,但可以修改
- 在任何时间点,它包含一些特定的字符序列
- 但可以通过某些方法调用来更改序列的长度和内容
- 简单来说:就是一个线程安全的可变字符串
- StringBuffer和String的区别
- 前者长度和内容都可以改变,后者不可改变
- 前者可以提前给出缓冲区,可以进行字符串拼接不会重新开辟空间;后者会开辟新的空间,会浪费太多资源
- StringBuffer的构造方法
- public StringBuffer()
- public StringBuffer(int capacity)
- public StringBuffer(String str)
- StringBuffer的方法
- public int capacity()返回当前容量(容量是新插入字符可用的存储量,超过此值将进行重新分配)
- public int length()返回长度(字符数)
- 线程安全简单描述:
- 线程是安全的;安全,代表着有同步存在,数据是安全的,效率不高
- 不安全,不同步,效率高
- 安全问题和效率问题,一直都是一个矛盾点
- 安全:售票、取号、银行业务(都是一个一个进行的)
- 效率:论坛(可以同步进行的)
StringBuffer的构造方法与方法的使用
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println("sb:"+sb);
System.out.println(sb.capacity());
System.out.println(sb.length());
System.out.println("**********************************");
StringBuffer sb2 = new StringBuffer(50);
System.out.println("sb2:"+sb2);
System.out.println(sb2.capacity());
System.out.println(sb2.length());
System.out.println("**********************************");
StringBuffer sb3 = new StringBuffer("hello");
System.out.println("sb3:"+sb3);
System.out.println(sb3.capacity());
System.out.println(sb3.length());
}
StringBuffer类的成员方法
添加功能
- public StringBuffer append(String str)
可以把任意类型的数据添加到字符串缓冲区中,并且返回的是字符串缓冲区本身 - public StringBuffer insert(int offset,String str)
在指定位置插入数据到字符串缓冲区
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
StringBuffer sb2 = sb.append("hello");
System.out.println(sb);
System.out.println(sb2);
System.out.println(sb == sb2);
sb.append(true);
sb.append("world");
sb.append(123);
System.out.println(sb);
System.out.println(sb2);
sb.append(12.34f).append("java").append(23).append(false);
System.out.println(sb);
System.out.println(sb2);
}
}
删除功能
- public StringBuffer deleteCharAt(int index)
- public StringBuffer delete(int start,int end)
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append(true).append(12).append(12.34f).append("hello");
System.out.println(sb);
System.out.println("**********************************");
sb.delete(0,4);
System.out.println(sb);
System.out.println("**********************************");
sb.deleteCharAt(2);
System.out.println(sb);
System.out.println("**********************************");
sb.delete(0,sb.length());
System.out.println(sb);
System.out.println("**********************************");
}
替换功能
- public StringBuffer replace(int start,int end,String str)
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append(12).append(true).append("hello").append(34.56f);
System.out.println(sb);
System.out.println("*********************************");
sb.replace(2,5,"world");
System.out.println(sb);
}
}
反转功能
- public StringBuffer reverse()(如果序列中包含任何替代对,则将它们视为单个字符进行反向操作)
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("StringBuffer");
sb.reverse();
System.out.println(sb);
}
}
截取功能
- public String substring(int start)
- public String substring(int start,int end)
截取功能和前面几个功能的不同:返回值是String类型,本身没有发生改变
从下面的举例代码中我们可以看到,输出的sb1是String类型,本身的sb并没有发生任何变化
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append(12).append("hello").append(12.34f).append(true);
System.out.println(sb);
System.out.println("********************************");
String sb1 = sb.substring(0, 4);
System.out.println(sb1);
System.out.println("********************************");
System.out.println(sb);
}
}
StringBuffer类练习
String和StringBuffer的相互转换
public static void main(String[] args) {
String s = "hello";
StringBuffer sb = new StringBuffer(s);
System.out.println(sb);
StringBuffer sb1 = new StringBuffer().append(s);
System.out.println(sb1);
StringBuffer sb3 = new StringBuffer("helloworld");
String s4 = new String(sb3);
System.out.println(s4);
String s5 = sb3.toString();
System.out.println(s5);
- List item
helloworld
把数组拼接成一个字符串
public static void main(String[] args) {
int [] arr = new int[]{1,2,3,4,5};
String s = "";
s += "[";
for(int i=0;i<arr.length;i++){
if(i==arr.length-1){
s += arr[i];
s += "]";
}else{
s += arr[i];
s += ",";
}
}
System.out.println(s);
StringBuffer sb = new StringBuffer();
sb.append("[");
for(int i=0;i<arr.length;i++){
if(i==arr.length-1){
sb.append(arr[i]);
sb.append("]");
}else{
sb.append(arr[i]);
sb.append(",");
}
}
System.out.println(sb);
}
把字符串反转
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据");
String s = sc.next();
String result = "";
char[] chars = s.toCharArray();
for(int i=s.length()-1;i>=0;i--){
result += chars[i];
}
System.out.println(result);
System.out.println("*******************************");
StringBuffer sb = new StringBuffer(s);
StringBuffer reverse = sb.reverse();
String s1 = new String(reverse);
System.out.println(s1);
System.out.println("*******************************");
System.out.println("链式编程改进");
System.out.println(new StringBuffer(s).reverse().toString());
}
}
判断一个字符串是否是对称字符串
(例如"abc"不是字符串,“aba”,“abba”,“aaa”,"mnanm"是对称字符串)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入你想判断的字符串");
String next = sc.next();
char[] chars = next.toCharArray();
boolean flag = true;
StringBuffer sb = new StringBuffer(next);
StringBuffer sb1 = sb.reverse();
String s = sb1.toString();
String s1 = new StringBuffer(next).reverse().toString();
if(s.equals(next)){
System.out.println("是对称字符串");
}else{
System.out.println("不是对称字符串");
}
}
面试题:String、StringBuffer、StringBuilder类的区别
1、String、StringBuffer、StringBuilder的区别 - String的内容是不可变的,而StringBuffer、StringBuilder的内容是可变的 - StringBuffer是同步线程安全的,数据安全,效率低 - StringBuilder是不同步的,数据不安全,效率高 2、StringBuffer和数组的区别 两个都可以看作是一个容器,装一些数据,但是,StringBuffer最终存储的数据是一个字符串数据,而数组可以存放多宗数据,但是同一个数组里面的元素类型一致
看程序写结果
- String作为参数传递
- StringBuffer作为参数传递
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
System.out.println(s1+"---"+s2);
change(s1,s2);
System.out.println(s1+"---"+s2);
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("world");
System.out.println(sb1+"---"+sb2);
change2(sb1,sb2);
System.out.println(sb1+"---"+sb2);
}
public static void change2(StringBuffer sb1,StringBuffer sb2){
sb1 = sb2;
sb2.append(sb1);
}
public static void change(String s1,String s2){
s1 = s2;
s2 = s1+s2;
}
感谢阅读,我是啊帅和和,一位大数据专业即将大四学生,祝你快乐。
|