字符串方法 java_Java String字符串方法

1.String构造函数

1> String()

2> String(char[] chars)

String(char[] chars,int startIndex,int numChars)

3> String(String strObj)

4> String(byte asciiChars[])

String(byte asciiChars[],int startIndex,int numChars)

2.整型、字符串相互转换

1> String -> int

(1)int i=Integer.parseInt(String s)

(2)int i=Integer.valueOf(str).intValue()

2>int -> String

(1)String s=String.valueOf(i)

(2)String s=Integer.toString(i)

(3)String s=””+i;

代码

//String  -->  int

String m_str="123";

int i=Integer.parseInt(m_str);

System.out.println("number is :"+(i+1));

i=Integer.valueOf(m_str);

System.out.println("number is :"+(i+2));

//String  -->  double

m_str="123.4";

Double d=Double.parseDouble(m_str);

System.out.println("number is :"+(d+3));

//int  -->  String

int j=321;

String s=String.valueOf(j);

System.out.println("string is :"+s+1);

s=Integer.toString(j);

System.out.println("string is :"+s+2);

s=""+j;

System.out.println("string is :"+s+3);

截图

8678401646abd827b632076c86368fb6.png

3.字符串处理函数

1>提取子串

String substring(int startIndex)

String substring(int startIndex,int endIndex)

截图

81e2339924bc4812b59614eb27df188f.png

2>字符串连接

String concat(String str)

代码

String str="live";

String ss;

ss=str.concat(" nihao");

System.out.println(ss);

System.out.println(str);

结果

live nihao

live

截图

40a92c0ed1f9b20ee59e88f749a04f72.png

3>字符串长度

int length()

4>求字符串中某一位置的字符

char charAt(int index)

代码

String str="powerful";

char c=str.charAt(3);

System.out.println(c);

System.out.println(str);

结果

e

powerful

5>字符串的比较  (StringBuffer未重写equals方法)

(1) int compareTo(String anotherString)

代码

String str="powerful";

String cstr="powerful";

String costr="poaer";

String comstr="pozes";

if(str.compareTo(cstr)==0)

{

System.out.println("str==cstr");

}

else

{

System.out.println("str!=cstr");

}

if(str.compareTo(costr)>0)

{

System.out.println("str>costr");

}

else if(str.compareTo(costr)<0)

{

System.out.println("str

}

else

{

System.out.println("str==costr");

}

if(str.compareTo(comstr)>0)

{

System.out.println("str>comstr");

}

else if(str.compareTo(comstr)<0)

{

System.out.println("str

}

else

{

System.out.println("str==comstr");

}

结果

str==cstr

str>costr

str

截图

98bad8cec3154e5268f723a4c050e51f.png

(2) boolean equals(Object anObject)   //大小写不相等

代码

String str="powerful";

String cstr="powerful";

String costr="poaer";

if(str.equals(cstr))

{

System.out.println("str==cstr");

}

else

{

System.out.println("str!=cstr");

}

if(str.equals(costr))

{

System.out.println("str==costr");

}

else

{

System.out.println("str!=costr");

}

结果

str==cstr

str!=costr

截图

c46f5ddf4b3780edfc79956a54312e95.png

(3) boolean equalsIgnoreCase(String anotherString)

代码

String str="powerful";

String cstr="poWErFul";

if(str.equalsIgnoreCase(cstr))

{

System.out.println("str==cstr");

}

else

{

System.out.println("str!=cstr");

}

截图

6ab486bac3026fa7aa78c05c6f9008a7.png

(4) regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)

regionMatches(boolean ignoreCase ,int startIndex,String str2,int str2StartIndex,int  numChars)

代码

String str="Appl pineApple ppEN ";

String s="Apple";

if(str.regionMatches(9,s,0,s.length()))

{

System.out.println("str中子串与s相等");

}

else

{

System.out.println("str中子串与s不等");

}

if(str.regionMatches(5,s,0,s.length()))

{

System.out.println("str中子串与s相等");

}

else

{

System.out.println("str中子串与s不等");

}

结果

str中子串与s相等

str中子串与s不等

截图

4a03e606c8b16f7635f9d6fc06cc86bc.png

代码

String str="Appl pineApple ppEN ";

String s="apple";

if(str.regionMatches(true,9,s,0,s.length()))

{

System.out.println("str中子串与s相等");

}

else

{

System.out.println("str中子串与s不等");

}

if(str.regionMatches(9,s,0,s.length()))

{

System.out.println("str中子串与s相等");

}

else

{

System.out.println("str中子串与s不等");

}

截图

a283d6c7101e33b29b67bfbc4b43de4c.png

6>判断字符串的前缀和后缀

(1)boolean startsWith(String prefix)   区分大小写

代码

String string = "powerful";

String preString="power";

if(string.startsWith(preString))

{

System.out.println("preString是string的前缀");

}

else

{

System.out.println("string的前缀不是preString");

}

截图

a53575bfeb3fedc4716c6ee00865910d.png

(2) boolean startsWith(String prefix, int toffset)

代码

String string = "powerful";

String preString = "wer";

if (string.startsWith(preString, 2)) {

System.out.println("preString是string的前缀");

} else {

System.out.println("string的前缀不是preString");

}

截图

cd3f2cada651f821d663670c91ad24fb.png

(3) boolean endsWith(String suffix)

代码

String string = "powerful";

String afterString = "ful";

if (string.endsWith(afterString)) {

System.out.println("afterString是string的后缀");

} else {

System.out.println("string的前缀不是afterString");

}

截图

3eb52ba20d1ab49ce433d43bc5e4848d.png

7>字符串单个字符的查找

(1) int indexOf(char ch)

代码

String str="powerful";

int c=str.indexOf(4);

System.out.println(c);

c=str.indexOf('e');

System.out.println(c);

截图

73b9eddca717e3e5b26ce5b88585e376.png

(2) int indexOf(char ch, int fromIndex)

代码

String str="pineapple len";

int c=10;

c=str.indexOf('p');

System.out.println(c);

c=str.indexOf('p',2);

System.out.println(c);

c=str.indexOf('p',4);

System.out.println(c);

c=str.indexOf('p',6);

System.out.println(c);

c=str.indexOf('p',7);

System.out.println(c);

截图

84337ba147f28441cb084a41a29c4bb1.png

(3) int lastIndexOf(char ch)

int lastIndexOf(char ch, int fromIndex)

代码

String str="an pineapple pen";

int c=10;

c=str.lastIndexOf('p');

System.out.println(c);

c=str.lastIndexOf('p',2);

System.out.println(c);

c=str.lastIndexOf('p',4);

System.out.println(c);

c=str.lastIndexOf('p',8);

System.out.println(c);

c=str.lastIndexOf('p',11);

System.out.println(c);

截图

d92f50e3c76d09d959d3279c200176e4.png

8>字符串子串的查找

(1) int indexOf(String str)

int indexOf(String str, int fromIndex)

代码

String str="apple pineapple pen";

int c=10;

c=str.indexOf("le");

System.out.println(c);

c=str.indexOf("le",5);

System.out.println(c);

c=str.indexOf("le",14);

System.out.println(c);

c=str.indexOf("el");

System.out.println(c);

截图

73b0f0a9d42b977f85b3c355b4e8602f.png

(2) int lastIndexOf (String str)

int lastIndexOf (String str, int fromIndex)

代码

String str="apple pineapple pen";

int c=10;

c=str.lastIndexOf ("le");

System.out.println(c);

c=str.lastIndexOf ("le",8);

System.out.println(c);

c=str.lastIndexOf ("le",14);

System.out.println(c);

c=str.lastIndexOf ("le",2);

System.out.println(c);

c=str.lastIndexOf ("el");

System.out.println(c);

截图

c0f4f185522a9b88927d847d1737ff26.png

9>字符串中字符大小写的转换

(1) String toLowerCase()  转换成小写

(2) String toUpperCase()   转换成大写

代码

String str="Apple pineApple PEN";

String upperStr=str.toUpperCase();//大写

String lowerStr=str.toLowerCase();//小写

System.out.println("原始:"+str);

System.out.println("大写:"+upperStr);

System.out.println("小写:"+lowerStr);

截图

9d6f1108ba5130ccee892b45ebaa4b47.png

10>字符串中多余空格的去除

String trim()  //仅是去除开头和结尾的空格,字符串里的空格保留

代码

String str=" Apple pineApple PEN   ";

String trimStr=str.trim();

System.out.println("an"+str+"u like");

System.out.println("an"+trimStr+"u like");

截图

bd14b0edb38cec37dc02ebde831a7eb1.png

11>字符串中字符的替换

(1) String replace(char oldChar,char newChar)

代码

String str=" Apple pineApple PEN   ";

String replaceStr=str.replace('p','o');

System.out.println("an"+str+"u like");

System.out.println("an"+replaceStr+"u like");

截图

3fe0566875a50ebffb9661e59e6a65d6.png

(2) String replaceFirst(String regex, String replacement)

代码

String str=" Apple pineApple PEN   ";

String replaceStr=str.replaceFirst("pp","live");

System.out.println("an"+str+"u like");

System.out.println("an"+replaceStr+"u like");

结果

an Apple pineApple PEN   u like

an Alivele pineApple PEN   u like

截图

13aeaffe4f27b21a83720d81d892c4cd.png

(3) String replaceAll(String regex, String replacement)

代码

String str=" Apple pineApple ppEN ";

String replaceStr=str.replaceAll("pp","live");

System.out.println("an"+str+"u like");

System.out.println("an"+replaceStr+"u like");

结果

an Apple pineApple ppEN u like

an Alivele pineAlivele liveEN u like

截图

78c6f14d0c01cf51c3b2a6c5ea835d9f.png

12>字符串转换成字符数组

toCharArray ()

代码

String str="Appl pineApple 苹果";

char[] ch=str.toCharArray();

System.out.println("str:"+str);

System.out.println("ch:"+ch);

System.out.println("ch:");

for(int i=0;i

{

System.out.print(ch[i]);

}

System.out.println();

System.out.println("ch[7]:"+ch[7]);

System.out.println("ch[16]:"+ch[16]);

结果及注意

str:Appl pineApple 苹果

ch:[C@15db9742

ch:

Appl pineApple

ch[7]:n

此结果得出的非常不稳定,不知道是不是sublime text软件的问题,还是程序的问题?(欢迎每个人去测试,然后评论,大家一起学习进步)

截图

cd591df3e05fbdbed3a517dcfa7bd874.png

8734e588cac7864c6649c6d14bb96b82.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值