public class test { public static void main(String[] args) { String s1="hello"; String s2="world"; String s3="hello"; System.out.println(s1==s3); s1=new String("hello"); s2=new String("hello"); System.out.println(s1==s2); System.out.println(s1.equals(s2)); char c[]={'s','u','n',' ','j','a','v','a'}; String s4=new String(c); String s5=new String(c,4,4); System.out.println(s4);//sun java System.out.println(s5);//java } }
输出:
true
false
true
sun java
java
String类常用方法
public class test { public static void main(String[] args) { String s1="sun java",s2="Sun Java"; System.out.println(s1.charAt(1)); System.out.println(s1.length()); System.out.println(s1.indexOf("java")); System.out.println(s1.indexOf("Java")); System.out.println(s1.equals(s2)); System.out.println(s1.equalsIgnoreCase(s2)); String s="我是程序员,我在学java"; String sr=s.replace('我','你'); System.out.println(sr); } }
输出:
u
8
4
-1
false
true
你是程序员,你在学java
public class test { public static void main(String[] args) { String s="Welcome to Java World!"; String s1=" sun java "; System.out.println(s.startsWith("Welcome")); System.out.println(s.endsWith("World!")); System.out.println(s.endsWith("World")); String sL=s.toLowerCase(); String sU=s.toUpperCase(); System.out.println(sL); System.out.println(sU); String subS = s.substring(11);//从第11位开始 System.out.println(subS); String sp=s1.trim(); System.out.println(sp); } }
输出:
true
true
false
welcome to java world!
WELCOME TO JAVA WORLD!
Java World!
sun java
public class test { public static void main(String[] args) { int j=1234567; String sNumber=String.valueOf(j);//将基本数据类型转化为字符串 System.out.println("j是"+sNumber.length()+"位数。"); String s="Mary,F,1976"; String[] sPlit=s.split(",");//分隔 for(int i=0;i<sPlit.length;i++) { System.out.println(sPlit[i]); } } }
输出:
j是7位数。
Mary
F
1976
//编写一个程序,输出一个字符串中的大写英文字符数,小写英文字母数以及非英文字母数 public class test { public static void main(String[] args) { String s="hadsjkhkASDJKSDHjkjS*&*&*(&(*?SD"; int MaxCount=0; int MinCount=0; int OthCount=0; for(int i=0;i<s.length();i++) { char t=s.charAt(i); if(t>='A'&t<='Z') { MaxCount++; } else if(t>='a'&t<'z') { MinCount++; } else { OthCount++; } } System.out.println(MaxCount+" "+MinCount+" "+OthCount); } }
输出:
11 11 10
另解:
//编写一个程序,输出一个字符串中的大写英文字符数,小写英文字母数以及非英文字母数 public class test { public static void main(String[] args) { int lCount=0,uCount=0,oCount=0; String s="SDJKJSHDkjndjaskjhadskjjadkshkjads&^*&^S*&^DS&S*"; String sL="abcdefghijklmnopqrstuvwxyz"; String sU="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(int i=0;i<s.length();i++) { char c=s.charAt(i); if(sL.indexOf(c) !=-1 ) {//在字符串里已经找到这个字母了,就是小写了 lCount++; }else if(sU.indexOf(c)!=-1) { uCount++; } else { oCount++; } } System.out.println(lCount+" "+uCount+" "+oCount); } }
输出:
26 12 10
方法三:
//编写一个程序,输出一个字符串中的大写英文字符数,小写英文字母数以及非英文字母数 public class test { public static void main(String[] args) { int lCount=0,uCount=0,oCount=0; String s="SDJKJSHDkjndjaskjhadskjjadkshkjads&^*&^S*&^DS&S*"; String sL="abcdefghijklmnopqrstuvwxyz"; String sU="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(int i=0;i<s.length();i++) { char c=s.charAt(i); if(Character.isLowerCase(c) ) {//在字符串里已经找到这个字母了,就是小写了 lCount++; }else if(Character.isUpperCase(c)) { uCount++; } else { oCount++; } } System.out.println(lCount+" "+uCount+" "+oCount); } }
输出:
26 12 10
public class test { public static void main(String[] args) { String s="sunjavajavajavasdaadasjajavajavadsdjava"; String sToFind="java"; int count=0; int index=-1; while((index=s.indexOf(sToFind))!=-1) { s=s.substring(index+sToFind.length()); count++; } System.out.println(count); } }
输出:
6
StringBuffer类举例
public class test { public static void main(String[] args) { String s="Microsoft"; char[] a={'a','b','c'}; StringBuffer sb1=new StringBuffer(s); sb1.append('/').append("IBM").append('/').append("Sun"); System.out.println(sb1); StringBuffer sb2=new StringBuffer("数字"); for(int i=0;i<=9;i++) { sb2.append(i); } System.out.println(sb2); sb2.delete(8, sb2.length()).insert(0, a); System.out.println(sb2); System.out.println(sb2.reverse()); } }
输出:
Microsoft/IBM/Sun
数字0123456789
abc数字012345
543210字数cba
public class test { public enum MyColor {red,green,blue}; public static void main(String[] args) { MyColor m=MyColor.green; switch(m) { case red: System.out.println("red"); break; case green: System.out.println("green"); break; default: System.out.println("default"); } System.out.println(m); } }
输出:
green
green