字符串

一、使用String对象存储字符串;
String S="hello qorld";
String S=new String();
String S=new String("hello world");


二、String 类位于java.lang包中,具有丰富的方法
计算字符串的长度、比较字符串、链接字符串、提取字符串


三、计算字符串长度

语法:
方法原型:
public int length(){

}

调用方法:
字符串标识符.length();

四、字符串比较
String类提供了equals()方法,比较存储在两个字符串对象的内容是否一致。
==:判断两个字符串在内存中的首地址,及判断是否是同一个字符串对象;

五、字符串大小写问题:

使用equalslgnoreCase()方法:不区分大小写

使用toLowerCase()方法:返回字符串的小写形式

使用toUpperCase()方法:返回字符串的大写形式


六、字符串链接:

方法1、使用“+”

方法2、使用String类的concat()方法;

七、字符串搜索方法:

public int indexOf(int ch) 搜索第一个出现的字符
public int indexOf(String value) ch(或字符串value)

public int lastindexOf(int ch) 搜索最后一个出现的字符ch
public int lastindexOf(String value) (或字符串 value)

public int lastindexOf(ch,formindex) 从指定位置开始找,跳过formindex开始找

s.endswith(字符串); 判断字符串是否以指定的格式结尾,返回boolean类型
s.startswith(字符串); 判断字符串是否以指定的格式开头,返回boolean类型;


八、字符串提取:


public String substring(int index) 截取从index开始一直到最后
public String substring(int beginindex,int endindex) 截取从beginindex开始,endindex-1结束
public String trim() 去掉前后空格


九:字符串拆分:
String类提供了split()方法,将一个字符串分割为子字符串,结构作为字符串数组返回

十:StringBuffer的使用:

sb.toString(); 装化为String类型

sb.append(""); 追加字符;

sb.insert(i,""); 插入字符串

//字符串链接
//方法一、“+”
String a1="123";
String a2="456";
String a3=a1+a2;
String a4="123"+"456";
System.out.println(a3);
System.out.println(a4);

//方法二、concat()
String b1="abc";
String b2="efg";
String b3=b1.concat(b2);
String b4="abc".concat("efg");
System.out.println(b3);
System.out.println(b4);

//字符串查询
String s="fahufahfiuhgahqhqirqignjqakgnvgjsnvifugsigiuhrhq89yrahgviujshgviguhus";
int index=s.indexOf('8'); //从前往后找(字符)
int index1=s.indexOf("fiu");//从前往后找(字符串)
System.out.println(index);
System.out.println(index1);

index=s.lastIndexOf("89"); //从后往前找(字符串)
System.out.println(index);
index=s.lastIndexOf('9'); //从后往前找(字符)
System.out.println(index);

System.out.println(s.indexOf('s',23)); //跳过23个字符,往后找
System.out.println(s.lastIndexOf('a',12));

String finle="jwsgjw.txt";
//s.endswith(字符串);
//判断字符串是否以指定的格式结尾,返回boolean类型
System.out.println(finle.endsWith(".txt"));

//s.startswith(字符串);
//判断字符串是否以指定的格式开头,返回boolean类型;
String name="heyuqi";
System.out.println(name.startsWith("he"));



//字符串提取
String names=new String("床前明月光");
System.out.println(names.substring(2)); //从下标为2开始一直到最后,都提取出来;


System.out.println(names.substring(1,5)); //从下标1开始到下标4,都提取出来。

String names1=new String(" 疑 是 地 上 霜 ");
System.out.println(names1);
System.out.println(names1.trim()); //去掉前后空格



//字符串拆分
String nameq=new String("喜洋洋/ 懒洋洋/沸羊羊/美羊羊/灰太狼/别看我就是一只羊");
System.out.println(nameq);
String [] word=nameq.split("/");
for(int i=0;i<word.length;i++){
System.out.println(word[i]);
}

System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();
// StringBuffer 使用
StringBuffer sb=new StringBuffer();//StringBuffer 创建

// sb.append();追加字符
sb.append("喜羊羊");
StringBuffer sb1=sb.append("懒羊羊").append("灰太狼");
System.out.println(sb1);

//sb.toString(); 转化为String类型
String sb2=sb1.toString();
System.out.println(sb2);


// sb.insert(); 插入字符
Scanner input=new Scanner(System.in);
System.out.print("请输入洋洋:");
String ss=input.next();
StringBuffer sss=new StringBuffer(ss);
for(int i=sss.length()-3;i>0;i=i-3){
sss.insert(i, "/");
}
System.out.println(sss);

原文地址:https://www.cnblogs.com/heyuqi/p/6986070.html