1.String类
java.lang.String 类代表不可变的字符序列。
“xxxxx”为该类的一个对象
String类的常见构造方法:
String(String original) 创建一个String对象为original的拷贝
String(char[] value) 用一个字符数组创建一个String对象
String(char[] value,int offset,int count) 用一个字符数组从offset项开始的count个字符序列创建一个String对象
结果是:
true
false
true
sun java
java
equals的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 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数组
举例:
WELCOME TO JAVA WORLD!
Java World!
sun java
3.常用方法(2)
静态重载方法
public static String valueOf() 可以将基本类型数据转换为字符串;例如:public static String valueOf(double d)方法
结果是:
j是7位数
Mary F 1976
统计字符串中大小写字母以及其他字符个数的程序:
结果是:7
优化程序:
java.lang.String 类代表不可变的字符序列。
“xxxxx”为该类的一个对象
String类的常见构造方法:
String(String original) 创建一个String对象为original的拷贝
String(char[] value) 用一个字符数组创建一个String对象
String(char[] value,int offset,int count) 用一个字符数组从offset项开始的count个字符序列创建一个String对象
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);
}
}
结果是:
true
false
true
sun java
java
equals的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 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 java
3.常用方法(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);
}
}