public class TestString { public static void main(String[] agrs){ String s1 = "hello"; String s2 = "Word"; String s3 = "hello"; System.out.PRintln(s1 == s3); //因为s1和s3并不是被new出来的 s1 = new String("hello"); s2 = new String("hello"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); //虽然是new出来的,但是String类对equals进行了重写 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); System.out.println(s5); }}结果是:truefalsetruesun javajavaequals的API:Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.比较这个字符串到指定的对象。如果该参数不为空,是一个字符串对象表示相同的字符序列作为该对象的结果是真的。2.String类常用方法public char charAt(int index) 返回字符串中第index个字符public int length () 返回字符串的长度public int indexOf(string str) 返回字符串中出现str的第一个位置public int indexOf(string str,int fromIndex) 返回字符串中从fromIndex开始出现str的第一个位置public int lastIndexOf(int ch) 查看ch字符最后出现的位置public int lastIndexOf(String str) 字符串public boolean equalsIgnoreCase(string another) 比较字符串与another是否一样(忽略大小写)public String replace(char oldChar,char newChar) 在字符串中用newChar字符替换oldChar字符举例:public class TestString { public static void main(String[] agrs) { 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 boolean starsWith(String prefix) 判断字符串是否以prefix字符串开头public boolean endsWith(String suffix) 判断字符串是否以suffix字符串解围public String toUpperCase() 返回一个字符串为该字符的大写形式public String toLowerCase() 返回一个字符串为该字符的小写形式public String substring(int beginIndex) 返回该字符串从beginIndex开始到结尾的子字符串public String substring(int beginIndex,int endIndex) 返回该字符串从beginIndex开始到endIndex结尾的字符串Public String trim() 返回将该字符串去掉开头和结尾空格后的字符串public String[] split (String regex,int limit) 将字符串分割成子字符串,返回字符串数组public byte[] getBytes() 将该字符串转换成byte数组举例:public class TestString { public static void main(String[] agrs) { String s = "Welcome to Java World!"; String s1 = " sun java "; System.out.println(s.startsWith("Welcome")); System.out.println(s.startsWith("World")); String sL = s.toLowerCase(); String sU = s.toUpperCase(); System.out.println(sL); System.out.println(sU); String subS = s.substring(11); System.out.println(subS); String sp = s1.trim(); System.out.println(sp); }}结果是:
true
false
welcome to java world!WELCOME TO JAVA WORLD!Java World!sun java3.常用方法(2)静态重载方法public static String valueOf() 可以将基本类型数据转换为字符串;例如:public static String valueOf(double d)方法public String[] split(String regex) 可以将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组。
public class TestString { public static void main(String[] agrs) { 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.print(sPlit[i] + " "); } }}结果是:j是7位数Mary F 1976统计字符串中大小写字母以及其他字符个数的程序:public class TestString { public static void main(String[] agrs) { int lCount = 0, uCount = 0, oCount = 0; String s = "advrsv^&jgjebjahvVHGFHBJHF^&*&fhaawd"; 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+"个"); }}结果是:
小写字母一共:21个,大写字母一共:9ge,其他字符一共:6个
输出一个字符串中部分字符段个数的程序:public class TestString { public static void main(String[] agrs) { String s = "sunjavajijavahpjavaokjavawljavahuijavavbjava"; int count = 0; String sToFind = "java"; int index = s.indexOf(sToFind); if (index != -1) { count++; } s = s.substring(index + sToFind.length()); while (s.indexOf(sToFind) != -1) { index = s.indexOf(sToFind); s = s.substring(index + sToFind.length()); count++; } System.out.println(count); }}结果是:7优化程序:public class TestString { public static void main(String[] agrs){ String s = "sunjavajijavahpjavaokjavawljavahuijavavbjava"; int count = 0; String sToFind = "java"; int index = -1; while((index = s.indexOf(sToFind)) != -1){ //给index赋值 s = s.substring(index + sToFind.length()); count++; } System.out.println(count); }}
新闻热点
疑难解答