public class StringTest{
public static void main(String[] args){
//1
String s1 = "abc";
//2
String s2 = new String("cdg");
//3
byte[] bytes={98,88,34,25};
String s3 = new String(bytes);
//4String(byte[] bytes, int offset, int length)Constructs a new String by decoding the specified subarray of bytes using the platform's default charse;
String s4 = new String(bytes,1,3);
//5.
char[] c1 = {'我','是','中','国','人'};
String s5 = new String(c1);
System.out.println(s5); //我是中国人
//6.
String s6 = new String(c1,2,2);
System.out.println(s6); //中国System.out.println(s3); //abcd String已经重写了Object中的toString
}
}
public class StringTest06{
public static void main(String[] args){
//1.char charAt(int index);
String s1 = "我是王勇,是坏人!";
char c1 = s1.charAt(2);
System.out.println(c1); //王
//2.boolean endsWith(String endStr);
System.out.println("HelloWorld.java".endsWith("java")); //true
System.out.println("HelloWorld.java".endsWith(".java")); //true
System.out.println("HelloWorld.java".endsWith("HelloWorld.java")); //true
System.out.println("HelloWorld.java".endsWith("txt")); //false
System.out.println("HelloWorld.java".endsWith("java ")); //false
//3. boolean equalsIgnoreCase(String anotherString);
System.out.println("abc".equalsIgnoreCase("ABc")); //true
//4.byte[] getBytes();
byte[] bytes = "abc".getBytes();
for(int i=0;i<bytes.length;i++){
System.out.println(bytes[i]);
}
//5.int indexOf(String str);
System.out.println("http://192.168.1.100:8080/oa/login.action?username=jack&pwd=123".indexOf("/oa"));//25
//6.int indexOf(String str, int fromIndex);
System.out.println("javaoraclec++javavb".indexOf("java",1)); //13
//7.int lastIndexOf(String str)
System.out.println("javaoraclec++javavb".lastIndexOf("java")); //13
//8.int lastIndexOf(String str, int fromIndex)
System.out.println("javaoraclec++javavb".lastIndexOf("java",14)); //13
//9.int length();
System.out.println("abc".length()); //数组是length属性,String是length()方法
//10. String replaceAll(String s1,String s2);
//mysqloraclec++mysqlvb
System.out.println("javaoraclec++javavb".replaceAll("java","mysql")); //这个程序是4个字符串
//11.String[] split(String s);
String myTime = "2008,08,08";
String[] ymd = myTime.split(",");
for(int i=0;i<ymd.length;i++){
System.out.println(ymd[i]);
}
//12.boolean startsWith(String s);
System.out.println("/system/login.action".startsWith("/")); //true
//13.String substring(int begin);
System.out.println("/oa/login.action".substring(3)); // /login.action
//14. String substring(int beginIndex, int endIndex)
System.out.println("/oa/login.action".substring(4,9)); //login
//15.char[] toCharArray();//字符串到字符数组的转化
char[] c2 = "我是李海波".toCharArray();
for(int i=0;i<c2.length;i++){
System.out.println(c2[i]);
}
//16.转换成大写
System.out.println("Abcdef".toUpperCase());
//17.转换成小写
System.out.println("ABCDEf".toLowerCase());
//18.String trim();、、去掉两边的空格
System.out.print(" a bcd e ".trim());
System.out.println("TEST");
//19.String valueOf(Object obj); Returns the string representation of the Object argument.
Object o = null;
System.out.println(o); //不会,因为并不是直接调用toString方法,String.valueOf(Object)这个方法对空值进行处理了。
System.out.println(String.valueOf(o));
//System.out.println(o.toString()); //会出现空指针
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341