import java.util.StringTokenizer;
import static java.lang.String.valueOf;
/**
* @author liucong
* @date 20190708084116
*
*/
public class StringLeng {
public static void main(String[] args){
String str = "Hello World";
String str1 = "Hello World";
String str2 = "Hello My World";
String str3 = "123";
String str4 = " Hello My World ";
long long1 = 123123123;
//输出字符串
System.out.println("输出字符串");
System.out.println(str);
//获取str长度
System.out.println("获取str长度");
System.out.println(str.length());
//判断字符串前缀
System.out.println("判断字符串前缀");
System.out.println(str.startsWith("H"));
System.out.println(str.startsWith("h"));
//判断字符串后缀
System.out.println("判断字符串后缀");
System.out.println(str.endsWith("d"));
System.out.println(str.endsWith("D"));
//比较两个字符串
System.out.println("比较两个字符串");
System.out.println(str.equals(str1));
System.out.println(str.equals(str2));
//把字符串转化为相应的数值
System.out.println("把字符串转化为int型");
System.out.println(Integer.parseInt(str3));
System.out.println("把字符串转化为long型");
System.out.println(Long.parseLong(str3));
System.out.println("把字符串转化为float型");
System.out.println(Float.valueOf(str3).floatValue());
System.out.println("把字符串转化为double型");
System.out.println(Double.valueOf(str3).doubleValue());
//将数值转化为字符串
System.out.println("将数值转化为字符串");
System.out.println(getType(valueOf(1)));
System.out.println(valueOf(1));
//字符串检索
System.out.println("字符串检索,如果没有检索到,将返回-1:");
System.out.println(str.indexOf("o"));
//得到字符串的子字符串
System.out.println("从4处开始获取:");
System.out.println(str.substring(4));
System.out.println("从3到6中间的字符:");
System.out.println(str.substring(3,6));
//替换字符串中的字符,去掉字符串前后空格
System.out.println("替换字符串中的字符:");
System.out.println(str.replace("Hello","Hi"));
System.out.println("去掉字符串前后空格:");
System.out.println(str4.trim());
//分析字符串
StringTokenizer st = new StringTokenizer(str, " b");
//countTokens()方法仅能用在StringTokenizer方法之后
System.out.println("得到一共有多少个语言符号:"+st.countTokens());
System.out.println("分析字符串:");
//只要字符串还有语言符号将返回true,否则返回false
while(st.hasMoreElements()){
//得到一共有多少个语言符号
//逐个获取字符串中的数据
System.out.println(st.nextToken());
}
}
//获取变量类型方法
public static String getType(Object o){
//使用int类型的getClass()方法
return o.getClass().toString();
}
}