String类的构造方法:
1)String()
2)String(byte[])
3)String(byte[],int,int)
4)String(byte[],int,int,String)
5)String(byte[],String)
6)String(char[])
7)String(char[],int,int)
8)String(String)
9)String(StringBuffer)
10)String(String)
基本操作1:取字符串charAt(int index)
package com.study;
public class Test {
public static void testString(){
<strong>String str=new String("Hello world");
char c=str.charAt(0); //取字符串操作</strong>
System.out.print(c);
}
public static void main(String[] args) {
Test.testString();
}
}
输出:H
基本操作2:比较字符串compareTo(Object o)、compareTo(String str)、CompareToIgnore(String str)
package com.study;
public class Test {
public static void testString(){ <strong>//compareTo()和CompareToIgnore()返回值为0,则表示相同</strong>
String str=new String("Hello world");
String str2 ="Hello world";
<strong>System.out.println("compareTo:"+(str.compareTo(str2)==0));
System.out.println("compareToIgnoreCase:"+(str.compareToIgnoreCase(str2)==0));</strong>
}
public static void main(String[] args) {
Test.testString();<pre name="code" class="java">String s="you are a little boy!";
char[] c=new char[s.length()];
c=s.toCharArray();
System.out.print(c);
}} 输出结果:
compareTo:truecompareToIgnoreCase:true
基本操作3:连接字符串concat(String str)
String str=new String("01234");
String str2 ="5678";
System.out.println(str.concat(str2));
输出结果:012345678
基本操作4:字符数组转为字符串 copyValueOf(char[] data) 、 copyValueOf(char[] data,int offset,int count) ,String(char[] data)、String(char[] ,int,int)
char data[]={'a','b','c','d'};
String str=String.copyValueOf(data);
System.out.println(str);
输出结果:abcd
这样也行:
char data[]={'a','b','c','d'};
String str=new String(data);
System.out.println(str);
输出结果:abcd
还有这样:
char data[]={'a','b','c','.'};
String str=String.copyValueOf(data,1,2);
System.out.println(str);
输出结果:bc
还有啊:
char data[]={'a','b','c','.'};
String str=new String(data,1,2);
System.out.println(str);
输出结果:bc
基本操作5:字符串转为字符数组getChars(int beigin,int end,char[] data,int dstBegin)、toCharArray
String s="you are a little boy!";
char[] c=new char[s.length()];
s.getChars(0, s.length(), c, 0);
System.out.print(c);
输出结果:you are a little boy!
String s="you are a little boy!";
char[] c=new char[s.length()];
c=s.toCharArray();
System.out.print(c);
输出结果:you are a little boy!
基本操作6:判断字符串结束内容endsWith(String s)
同理:判断起始子串startsWith(String s),startsWith(String s,int toffset)
String s="you are a little boy!";
boolean b=s.endsWith("boy!");
System.out.print(b);
输出结果:true
基本操作7:字符串对象是否相等equals(Object o)、equalsIgnoreCase(String s)
String s="you are a little boy!";
boolean b1=s.equals("boy!");
boolean b2=s.equals("you are a little boy!");
boolean b3=s.equalsIgnoreCase("you are a little boy!");
System.out.print(b1+","+b2+","+b3);
输出结果:false,true,true
基本操作8:字符串中的字符的定位indexOf(int ch)、indexOf(int ch,int fromindex)、indexof(String s),indexOf(String s,int fromindex)
String s="you are a little boy!";
int pos =s.indexOf('a');
int pos1=s.indexOf("are");
int pos2=s.indexOf('y',2);
System.out.print(pos+"和"+pos1+"and"+pos2);
输出结果:4和4and19
基本操作9:字符串中的字符最后的位置lastIndexOf(int ch),lastIndexOf(int ch,int fromindex),lastIndexOf(String s),lastIndexOf(String s,int fromindex)
String s="you are a little boy!";
int pos =s.lastIndexOf('a');
int pos1=s.lastIndexOf("are");
int pos2=s.lastIndexOf('y',2);
System.out.print(pos+"和"+pos1+"and"+pos2);
输出结果:8和4and0
基本操作10:判断长度length()
int Len=(new Stirng("abc")).length();
基本操作11:字符串内容的替换replace()
String s="you are a little boy!";
s.replace('b', 'B');
System.out.println(s);
System.out.println(s.replace('b', 'B'));
输出结果:
you are a little boy!
you are a little Boy!
基本操作12:取子字符串subString(int begin),subString(int begin,int end)
String s="you are a little boy!";
System.out.println(s.substring(0));
System.out.println(s.substring(0,5));
输出结果:
you are a little boy!
you a
基本操作13:字符串大小写转换toUpperCase().toLowerCase()
String s="you are a little boy!";
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
输出结果:
YOU ARE A LITTLE BOY!
you are a little boy!
基本操作14:将其他类型转为String:valueOf()
int n=5;
char[] c={'a','b'};
String s1,s2;
s1=String.valueOf(n);
s2=String.valueOf(c);
System.out.print(s1+"和"+s2);
输出结果:5和ab