文章目录
前言
字符串广泛应用 在 Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串
一、一些常用的String方法
1.字符串长度
代码如下(示例):
public class sda {
public static void main(String[] args) {
String f="suyize";
System.out.println(f.length());
}
}
2.替换字符串
代码如下(示例):
public class sda {
public static void main(String[] args) {
String p1="suyixx";
String p2="ze";
System.out.println(p1.replace("xx",p2));
}
}
3.根据给定正则表达式的匹配拆分此字符串
代码如下(示例):
public class sda {
public static void main(String[] args) {
String q1="sb,sb,sb,sb,sb";
String[] oo=q1.split(",");
for(String p:oo){
System.out.println(p);
}
}
}
4.连接字符串
代码如下(示例):
public class sda {
public static void main(String[] args) {
String s1="su";
String s2="yize";
String s3=s1.concat(s2);
System.out.println(s1+s2);
}
}
5.截取字符串
代码如下(示例):
public class sda {
public static void main(String[] args) {
String k1="sdadwaeadsrads";
String k2=k1.substring(0,6);
System.out.println(k2);
}
}
二、StringBuffer和StringBuilder
1.StringBuffer
在使用 StringBuffer 类时,每次都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,所以如果需要对字符串进行修改推荐使用 StringBuffer。
代码如下(示例):
public class sda {
public static void main(String[] args) {
StringBuffer sBuffer = new StringBuffer("菜鸟教程官网:");
sBuffer.append("www");
sBuffer.append(".runoob");
sBuffer.append(".com");
System.out.println(sBuffer);
}
}
以上实例编译运行结果如下:
2.StringBuilder
StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问);由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。
代码如下(示例):
public class sda {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(10);
sb.append("Runoob.");
System.out.println(sb);
sb.append("!");
System.out.println(sb);
sb.insert(7, "Java");
System.out.println(sb);
sb.delete(5,7);
System.out.println(sb);
}
}
StringBuilder sb = new StringBuilder(10)
sb.append(“Runoob.”)
sb.append(“!”)
sb.insert(7, “Java”)
sb.delete(5,7)
一个小项目(学生信息查询系统)【简易版】
1.菜单(menu.java)
package StringDemo;
public class menu {
public static void Menu(){
System.out.println("****************************************************");
System.out.println(" 1.信息录入 ");
System.out.println(" 2.信息修改 ");
System.out.println(" 3.信息删除 ");
System.out.println(" 0.退出 ");
System.out.println("****************************************************");
}
}
2.学生信息种类(student.java)
package StringDemo;
public class Student {
private String name;
private String sex;
private String age;
private String phone;
private String garde;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age='" + age + '\'' +
", phone='" + phone + '\'' +
", garde='" + garde + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getGarde() {
return garde;
}
public void setGarde(String garde) {
this.garde = garde;
}
}
3.信息录入(app.java)
public static String addinfor(String info){
String[] arr = info.split(";");
Student laoliu=new Student();
laoliu.setName(arr[0]);
laoliu.setSex(arr[1]);
laoliu.setAge(arr[2]);
laoliu.setPhone(arr[3]);
laoliu.setGarde(arr[4]);
System.out.println(laoliu.toString());
return laoliu.toString();
}
4.信息修改(App.java)
public static int updateinfor(StringBuffer sb,String sjj){
String str=sb.toString();
String[] pl=str.split(",");
int end=0;
end=pl[0].length()+pl[1].length()+pl[2].length()+2;
int start=end-3;
sb.delete(start,end-1);
System.out.println(sb);
sb.insert(start,sjj);
System.out.println(sb);
return 0;
}
5.信息删除(App.java)
public static int delinfor(StringBuffer stu){
String str=stu.toString();
String[] pl=str.split(",");
int end=0;
end=pl[0].length()+pl[1].length()+pl[2].length()+2+pl[3].length()+pl[4].length()+4;
int start=0;
stu.delete(start,end);
return 0;
}
6.主函数(App.java)
public class App {
public static void main(String[] args) {
boolean pp=true;
String Y="T";
StringBuffer stu=new StringBuffer();
while(pp){
menu.Menu();
System.out.println("请输入操作:");
Scanner scanner=new Scanner(System.in);
int a=scanner.nextInt();
switch(a){
case 1:
System.out.println("请输入学生信息:");
String s=scanner.next();
stu.append( addinfor(s));
break;
case 2:
System.out.println("请输入修改的信息:");
String s0=scanner.next();
updateinfor(stu,s0);
break;
case 3:
System.out.println("您确定要删除信息吗?T/F");
String x=scanner.next();
if(0==Y.compareTo(x)){
delinfor(stu);
System.out.println("信息删除成功");
}else{
System.out.println(stu);
}
break;
case 0:
pp=false;
break;
default:
System.out.println("输入错误");
}
}
}
7.App.java整合
package StringDemo;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
boolean pp=true;
String Y="T";
StringBuffer stu=new StringBuffer();
while(pp){
menu.Menu();
System.out.println("请输入操作:");
Scanner scanner=new Scanner(System.in);
int a=scanner.nextInt();
switch(a){
case 1:
System.out.println("请输入学生信息:");
String s=scanner.next();
stu.append( addinfor(s));
break;
case 2:
System.out.println("请输入修改的信息:");
String s0=scanner.next();
updateinfor(stu,s0);
break;
case 3:
System.out.println("您确定要删除信息吗?T/F");
String x=scanner.next();
if(0==Y.compareTo(x)){
delinfor(stu);
System.out.println("信息删除成功");
}else{
System.out.println(stu);
}
break;
case 0:
pp=false;
break;
default:
System.out.println("输入错误");
}
}
}
//信息添加
public static String addinfor(String info){
String[] arr = info.split(";");
Student laoliu=new Student();
laoliu.setName(arr[0]);
laoliu.setSex(arr[1]);
laoliu.setAge(arr[2]);
laoliu.setPhone(arr[3]);
laoliu.setGarde(arr[4]);
System.out.println(laoliu.toString());
return laoliu.toString();
}
//信息修改
public static int updateinfor(StringBuffer sb,String sjj){
String str=sb.toString();
String[] pl=str.split(",");
int end=0;
end=pl[0].length()+pl[1].length()+pl[2].length()+2;
int start=end-3;
sb.delete(start,end-1);
System.out.println(sb);
sb.insert(start,sjj);
System.out.println(sb);
return 0;
}
//信息删除
public static int delinfor(StringBuffer stu){
String str=stu.toString();
String[] pl=str.split(",");
int end=0;
end=pl[0].length()+pl[1].length()+pl[2].length()+2+pl[3].length()+pl[4].length()+4;
int start=0;
stu.delete(start,end);
return 0;
}
}