一、前言
1、String表示字符串类型,属于 引用数据类型
,不属于基本数据类型。
2、在java中随便使用 双引号括起来
的都是String对象。
例如:“abc”,“def”,“hello world!”,这是3个String对象。
3、java中规定,双引号括起来的字符串,是 不可变
的,也就是说"abc"自出生到最终死亡,不可变,不能变成"abcd",也不能变成"ab"
4、在JDK当中双引号括起来的字符串,例如:“abc” "def"都是直接存储在“方法区”的“字符串常量池
”当中的。
5、为什么SUN公司把字符串存储在一个“字符串常量池”当中呢?
因为字符串在实际的开发中使用太频繁。为了执行效率,所以把字符串放到了方法区的字符串常量池当中。
eg1.
public class StringTest01 {
public static void main(String[] args) {
String s1 = "abcdef";
String s2 = "abcdef" + "xy";
String s3 = new String("xy");
}
}
eg2.
public class StringTest02 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2);
String x = new String("xyz");
String y = new String("xyz");
System.out.println(x == y);
}
}
注意:
字符串对象之间的比较不能使用“== ”,"=="不保险。应该调用String类的equals方法。
eg3.
String k = new String("testString");
System.out.println("testString".equals(k));
System.out.println(k.equals("testString"));
二、构造方法
构造方法名 | eg. |
---|
String s = “xxx” | 最常用 |
String(String original) | String(“xxx”) |
String(char数组) | |
String(char数组,起始下标,长度) | |
String(byte数组) | |
String(byte数组,起始下标,长度) | |
String(StringBuffer buffer) | |
String(StringBuilder builder) | |
eg.
class StringTest{
public static void main(String[] args) {
String s1 = "我是中国人";
System.out.println(s1);
String s2 = new String("我是中国人");
System.out.println(s2);
char[] c = {'我' , '是', '中', '果', '人'};
String s3 = new String(c);
System.out.println(s3);
String s4 = new String(c, 2, 3);
System.out.println(s4);
byte[] b = {65, 66 ,67, 68};
String s5 = new String(b);
System.out.println(s5);
String s6 = new String(b, 1, 2);
System.out.println(s6);
StringBuffer sb1 = new StringBuffer("我是福建人");
String s7 = new String(sb1);
System.out.println(s7);
StringBuilder sb2 = new StringBuilder("我是厦门人");
String s8 = new String(sb2);
System.out.println(s8);
}
}
三、方法
方法名 | 作用 |
---|
char charAt(int index) | 返回指定位置的字符 |
int compareTo(String anotherString) | 比较两个字符串。相等返回0;前大后小返回1;前小后大返回-1 |
boolean contains(CharSequence s) | 判断字符串是否包含s |
boolean endsWith(String suffix) | 判断字符串是否以suffix结尾 |
boolean equals(Object anObject) | 判断两个串是否相等 |
boolean equalsIgnoreCase(String anotherString) | 忽略大小写判断两个串是否相等 |
byte[] getBytes() | 将字符串串变成字节数组返回 |
int indexOf(String str) | 返回str在字符串第一次出现的位置 |
boolean isEmpty() | 字符串是否为空 |
int length() | 字符串长度 |
int lastIndexOf(String str) | 返回str最后一次出现的位置 |
String replace(CharSequence target, CharSequence replacement) | 用replacement替换字符串target的字符 |
String[] split(String regex) | 将字符串以regex分割 |
boolean startsWith(String prefix) | 判断字符串是否以prefix开始 |
String substring(int beginIndex) | 从beginIndex开始截取字串 |
String substring(int beginIndex, int endIndex) | 截取beginIndex到endIndex - 1的字符串 |
char[] toCharArray() | 将字符串转换乘char数组 |
String toLowerCase() | 字符串转小写 |
String toUpperCase() | 字符串转大写 |
String trim() | 去除字符串两边空格 |
静态方法 | |
static String valueOf(int i) | 将 i 转换成字符串 |
eg.
class StringTest{
public static void main(String[] args) {
String s1 = "hello world";
System.out.println(s1.charAt(6));
String s2 = "abc";
String s3 = "xyz";
String s4 = "xyz";
System.out.println(s2.compareTo(s3));
System.out.println(s3.compareTo(s4));
System.out.println(s4.compareTo(s1));
System.out.println(s2.equals(s3));
System.out.println(s1.endsWith("world"));
System.out.println(s1.endsWith("t"));
String s5 = "HELLO worLD";
System.out.println(s1.equalsIgnoreCase(s5));
byte[] b = s1.getBytes();
System.out.println(Arrays.toString(b));
System.out.println(s1.indexOf("world"));
System.out.println(s1.indexOf("h"));
System.out.println(s1.isEmpty());
System.out.println(s1.length());
String s6 = "javapythonc++cphpjavapython";
System.out.println(s6.lastIndexOf("java"));
System.out.println(s6.lastIndexOf("h"));
String s7 = "name=zhangsan&age=18&sex=男";
String newS7 = s7.replace("&", ";");
System.out.println(newS7);
String[] splitS7 = s7.split("&");
System.out.println(Arrays.toString(splitS7));
System.out.println(s6.startsWith("java"));
System.out.println(s6.startsWith("python"));
System.out.println(s6.substring(10));
System.out.println(s6.substring(10, 13));
char[] charS6 = s6.toCharArray();
System.out.println(Arrays.toString(charS6));
System.out.println(s6.toUpperCase());
System.out.println(s5.toLowerCase());
String s8 = " 你 好 世 界 ";
System.out.println(s8.trim());
System.out.println("------------------------------");
System.out.println(String.valueOf(123));
System.out.println(String.valueOf(3.14));
System.out.println(String.valueOf(true));
System.out.println(String.valueOf(new Object()));
}
}
注意:
1.为什么 System.out.println(引用) 会自动调用toString()方法?
因为println()会调用String.valueOf()方法而String.valueOf()会调用toString()方法
2.String byte数组 理解
byte[] b = new byte[1000000000];//有1000000000都是byte类型的
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)