String类中的构造方法
//1.
String s1="abcd";
//2.传byte数组
byte[] bytes={97,98,99,100};
String s2=new String(bytes); //s2="abcd"
System.out.println("s2="+s2);
//3.String(byte数组,数组元素起始下标位置,长度)
String s3=new String(bytes,1,2);
System.out.println("s3="+s3);
//4.传char数组
char[] chars={'我','是','中','国','人'};
String s4=new String(chars);
System.out.println("s4="+s4);
//5.String(char数组,数组元素起始下标位置,长度)
String s5=new String(chars,2,3);
System.out.println("s5="+s5);
//6.
String s6=new String("abcd");
System.out.println("s6="+s6);
String 类中的方法
- 1.char charAt(int index) 返回指定索引处的char值
- 2.int compareTo(String anotherString) 按字典顺序比较两个字符串。
- 3.boolean contains(CharSequence s) 判断当前的字符串是否包含CharSequence字符串,返回true或false
- 4.boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。返回true或false
- 5.boolean equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写。返回true或false
- 6. byte[] getBytes() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
- 7. int indexOf(String str) 返回指定子字符串在当前字符串中第一次出现处的索引。
- 8.boolean isEmpty() 当字符串为空时true。
- 9. int lastIndexOf(String str) 返回指定字符串在此字符串中最后一次出现处的索引。
- 10. int length() 返回此字符串的长度。
- 11. String replace(CharSequence target, CharSequence replacement) 把原字符串中的子字符串target替换为replacement字符串并返回一个新的字符串
- 12. String[] split(String regex) 拆分字符串
- 13.boolean startsWith(String prefix) 判断某个字符串是否以某个子字符串开始。
- 14. String substring(int beginIndex) 截取字符串;beginIndex是起始下标
- 15.String substring(int beginIndex, int endIndex) 截取字符串;beginIndex是起始下标(包含),endIndex是结束下标(不包含)
- 16.char[] toCharArray() 将此字符串转换为一个新的字符数组。
- 17.String toUpperCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为大写
- 18.String trim() 去除字符串的前后空白
- 19.static String valueOf(Object b) String中唯一一个静态方法;将非字符串转换为字符串
- 20.boolean equals(String s) 判断两个字符串是否相等
package StringDemo3;
import java.util.Locale;
public class TextMethod {
/**
* String中常用方法
* @param args
*/
public static void main(String[] args) {
System.out.println("1.----------------------------------------------------------------------");
//1.char charAt(int index) 返回指定索引处的char值
String s1=new String("中国人");
char c = s1.charAt(1);
System.out.println(c);
System.out.println("2.----------------------------------------------------------------------");
//2.int compareTo(String anotherString) 按字典顺序比较两个字符串。
int i = "abc".compareTo("abc");
System.out.println(i);//等于零,前后一致 99-99=0
int i1 = "abcd".compareTo("abce");
System.out.println(i1);//-1(小于零),前小后大 99-100=0
int i2 = "abce".compareTo("abcd");
System.out.println(i2);//1(大于零),前大后小 100-99=0
int i3 = "abc".compareTo("xyz");
System.out.println(i3);//-23 97-120
System.out.println("3.-----------------------------------------------------------------------");
//3.boolean contains(CharSequence s) 判断当前的字符串是否包含CharSequence字符串,返回true或false
System.out.println("hello.java".contains("java"));//true
System.out.println("4.-----------------------------------------------------------------------");
//4.boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。返回true或false
System.out.println("hello.java".contains(".java"));//true
System.out.println("5.-----------------------------------------------------------------------");
//5.boolean equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写。返回true或false
System.out.println("abc".equalsIgnoreCase("ABC"));//true
System.out.println("6.-----------------------------------------------------------------------");
//6. byte[] getBytes() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
byte[] bytes = "abcdef".getBytes();
for (byte aByte : bytes) {
System.out.println(aByte);
}
System.out.println("7.-----------------------------------------------------------------------");
//7. int indexOf(String str) 返回指定子字符串在当前字符串中第一次出现处的索引。
System.out.println("abcdef".indexOf("def"));//3
System.out.println("8.-----------------------------------------------------------------------");
//8.boolean isEmpty() 当字符串为空时true。
System.out.println("acv".isEmpty());
System.out.println("9.-----------------------------------------------------------------------");
//9. int lastIndexOf(String str) 返回指定字符串在此字符串中最后一次出现处的索引。
System.out.println("abcdefdefabc".lastIndexOf("def"));//6
System.out.println("10.-----------------------------------------------------------------------");
//10. int length() 返回此字符串的长度。
System.out.println("asfsdgfd".length());//8
System.out.println("11.-----------------------------------------------------------------------");
//11. String replace(CharSequence target, CharSequence replacement) 把原字符串中的子字符串target替换为replacement字符串并返回一个新的字符串
String ss="asdfghsdggfdhgfhfghfy";
String target="asdfgh";
String replacement="asdfghjkl";
String replace = ss.replace(target, replacement);
System.out.println(replace);//asdfghjklsdggfdhgfhfghfy
String replace1 = "name=张三&&age=20&&password=123".replace("=", ":");//把"="替换为":"
System.out.println(replace1);//name:张三&&age:20&&password:123
System.out.println("12.-----------------------------------------------------------------------");
//12. String[] split(String regex) 拆分字符串
String[] split = "1980-10-11".split("-");//以”-“拆分字符串,返回一个字符串数组
for (String s : split) {
System.out.println(s);//1980 10 11
}
String param="name:张三&&age:20&&password:123";
String[] split1 = param.split("&&");
for (String s : split1) {
System.out.println(s);//name:张三 age:20 password:123
}
System.out.println("13.-----------------------------------------------------------------------");
//13.boolean startsWith(String prefix) 判断某个字符串是否以某个子字符串开始。
boolean http = "http://www.baidu.com".startsWith("http");
System.out.println(http);//true
System.out.println("14.-----------------------------------------------------------------------");
//14. String substring(int beginIndex) 截取字符串;beginIndex是起始下标
String substring = "http://www.baidu.com".substring(7);
System.out.println(substring);//www.baidu.com
System.out.println("15.-----------------------------------------------------------------------");
//15.String substring(int beginIndex, int endIndex) 截取字符串;beginIndex是起始下标(包含),endIndex是结束下标(不包含)
String substring1 = "http://www.baidu.com".substring(7,10);
System.out.println(substring1);//www
System.out.println("15.-----------------------------------------------------------------------");
//16.char[] toCharArray() 将此字符串转换为一个新的字符数组。
char[] chars="我是中国人".toCharArray();
for (char aChar : chars) {
System.out.println(aChar);
}
System.out.println("16.-----------------------------------------------------------------------");
//16. String toLowerCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
String s = "sdfjhADFSFDtycFG".toLowerCase();
System.out.println(s);//sdfjhadfsfdtycfg
System.out.println("17.-----------------------------------------------------------------------");
//17.String toUpperCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为大写
String s2 = "sdfjhADFSFDtycFG".toUpperCase();
System.out.println(s2);//SDFJHADFSFDTYCFG
System.out.println("18.-----------------------------------------------------------------------");
//18.String trim() 去除字符串的前后空白
String trim = " Hello Word ".trim();
System.out.println(trim);//Hello Word
System.out.println("19.-----------------------------------------------------------------------");
//19.static String valueOf(Object b) String中唯一一个静态方法;将非字符串转换为字符串
boolean flag=false;
String s3 = String.valueOf(flag);
System.out.println(s3);//转换为false字符串
String s4 = String.valueOf(new User("李四",25));
System.out.println(s4);//对象转换为字符串,调用toString方法
System.out.println("20.-----------------------------------------------------------------------");
//20.boolean equals(String s) 判断两个字符串是否相等
boolean abc = "abc".equals("abc");
System.out.println(abc);
}
}
class User{
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
运行结果
1.----------------------------------------------------------------------
国
2.----------------------------------------------------------------------
0
-1
1
-23
3.-----------------------------------------------------------------------
true
4.-----------------------------------------------------------------------
true
5.-----------------------------------------------------------------------
true
6.-----------------------------------------------------------------------
97
98
99
100
101
102
7.-----------------------------------------------------------------------
3
8.-----------------------------------------------------------------------
false
9.-----------------------------------------------------------------------
6
10.-----------------------------------------------------------------------
8
11.-----------------------------------------------------------------------
asdfghjklsdggfdhgfhfghfy
name:张三&&age:20&&password:123
12.-----------------------------------------------------------------------
1980
10
11
name:张三
age:20
password:123
13.-----------------------------------------------------------------------
true
14.-----------------------------------------------------------------------
www.baidu.com
15.-----------------------------------------------------------------------
www
15.-----------------------------------------------------------------------
我
是
中
国
人
16.-----------------------------------------------------------------------
sdfjhadfsfdtycfg
17.-----------------------------------------------------------------------
SDFJHADFSFDTYCFG
18.-----------------------------------------------------------------------
Hello Word
19.-----------------------------------------------------------------------
false
User{name='李四', age=25}
20.-----------------------------------------------------------------------
trueProcess finished with exit code 0