javastring,java中stringbuilder
java中的string用法
java中的string用法,可以到java api查找方法的使用方式:
class?StringMethodDemo?
{
?public?static?void?method_Zhuanhuan_Qukong_Bijiao()
?{
??String?s?=?"?????hello?Java????";
??
??//打印结果是:(hello和java前后门都有空格)hello?java
??sop(s.toUpperCase());
??//打印结果是:(HELLO和JAVA前后门都有空格)HELLO?JAVA
??sop(s.toLowerCase());
??//打印及结果是:不带空格的“hello?java”
??sop(s.trim());
??//比较数的大写,打印结果是:1,因为b对应ascii值是98,
??//a对应是97,所以b-a=1
??String?s1?=?"abc";
??String?s2?=?"aaa";
??sop(s1.compareTo(s2));
?}
?public?static?void?method_sub()
?{
??String?s?=?"abcdef";
??//打印结果是:cdef,从指定位置开始到结尾。如果角标不存在,会出现字符串角标越界。
??sop(s.substring(2));
??//打印结果是:cd,包含头,不包含尾。
??sop(s.substring(2,4));
?}
?public?static?void?method_split()
?{
??String?s?=?"zhangsan,lisi,wangwu";
??String[]?arr?=?s.split(",");
??for(int?x=0;?xarr.length;?x++)
??{
???sop(arr[x]);
??}
?}
?public?static?void?method_replace()
?{
??String?s?=?"hello?java";
??//String?s1?=?s.replace('a','n');
??//String?s1?=?s.replace('w','n');??如果要替换的字符不存在,返回的还是原串
??
??String?s1?=?s.replace("java","world");//打印结果是:hello?world
??sop("s="+s);?//打印结果是:hello?java因为字符串一旦被初始化,值就不可被改变
??sop("s1="+s1);//打印结果是:hello?jnvn
?}
?public?static?void?method_trans()
?{
??char[]?arr?=?{'a','b','c','d','e','f'};
??String?s?=?new??String(arr,1,3);
??sop("s="+s);//打印结果是:bcd
??String?s1?=?"zxcvbnm";
??char[]?chs?=?s1.toCharArray();
??for(int?x=0;?xchs.length;?x++)
??{
???sop("ch="+chs[x]);//打印结果是:ch=z,x,c,v,b,n,m
??}
?}
?public?static?void?method_is()
?{
??String?str?=?"ArrayDemo.java";
?//判断文件名称是否是Array单词开头
??sop(str.startsWith("Array"));
?
?//判断文件名称是否是.java的文件
??sop(str.endsWith(".java"));
?
?//判断文件中是否包含Demo
??sop(str.contains("Demo"));
?}
?
?public?static?void?method_get()
?{
??String?str?=?"abcdeakpf";
??//长度
??sop(str.length());
??//根据索引获取字符
??sop(str.charAt(4));
??//sop(str.charAt(40));当访问到字符串中不存在的角标时会发生StringIndexOutOfBoundsException(字符串角标越界异常)
??//根据字符获取索引
??//sop(str.indexOf('a'));
??sop(str.indexOf('a',3));//打印的是5,因为角标3是d,
????????//所以从d后面开始找a,第5个角标是a
??//sop(str.indexOf('t',3))打印:-1,如果没有找到角标,返回-1
??
??//反向索引一个字符出现的位置(从右往左查找,但是角标还是从左开始)
??sop(str.lastIndexOf("a"));
?}
?public?static?void?main(String[]?args)?
?{
???method_Zhuanhuan_Qukong_Bijiao();
??//method_sub();
??//method_split();
??//method_replace();??
??//method_trans();?
??//method_is();
??//method_get();
??/*
??String?s1?=?"abc";
??String?s2?=?new?String("abc");
??String?s3?=?"abc";
??System.out.println(s1==s2);
??System.out.println(s1==s3);
??*/
?}
?public?static?void?sop(Object?obj)
?{
??System.out.println(obj);
?}
}

java string是什么类型
字符串类型,可以存放中文、英文、符号,以及数字,差不多什么都可以放。但不管发什么内容都会背视作为字符。比如int类型 1+1=2,而String类型"1"+"1"="11",他并不会进行四则运算。而是直接把两个字符组合起来。
java string常用方法
常见String类的获取功能
public int length(): 获取字符串的长度。
public char charAt(int index): 获取指定索引位置的字符
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。