package com.wyq.test;
public class TestSt {
public static void main(String[] args) {
String str = new String("helloworld");
//【1】获取指定位置上的字符char charAt(int index),实际上就是根据索引获取char类型的数组value的值 value【index】
for(int i = 0;i<str.length();i++){
System.out.println(str.charAt(i));//charSequence接口中的方法
}
//【2】int compareTo(String str)比价两个字符串的大小 comparable接口中的方法
System.out.println("apple.compareTo(cat)"+("apple".compareTo("cat")));
System.out.println("apple.compareTo(apple)"+("apple".compareTo("apple")));
System.out.println("cat.compareTo(apple)"+("cat".compareTo("apple")));
String str1 = "aanana";
String str2 = "banana";
String str3 = "can";
System.out.println("str1.compareTo(str2)"+(str1.compareTo(str2)));
System.out.println("str1.compareTo(str2)"+(str1.compareTo(str3)));
/**
* compareTo的比较,默认是按照首字母进行比较的
*/
//【3】int compareToIgnoreCase(String str )忽略大小写
System.out.println("apple".compareToIgnoreCase("APPLE"));
//【4】string concat (String),拼接
String str4 = "apple";
String str5 = str4.concat(str2);
System.out.println(str5);
System.out.println(str5.concat("China"));
//【5】boolean contains(CharSequence ch ) 接口作方法的形式参数 , 可以传入接口的任何实现类
System.out.println(str5.contains("ba"));
System.out.println(str5.contains(str2));
//【6】boolean endsWith(String str ) 判断是否以指定的字符 str 结尾,用于判断是否是指定类型的文件
System.out.println(str5.endsWith("na"));
System.out.println(str5.endsWith(str2));
System.out.println("iloveyou".endsWith("you"));
//[7]boolean equals(Object obj ) 父类 Object 作为方法的形式参数 ,Object 类中的方法
System.out.println("apple".equals("APPLE"));
System.out.println("apple".equals(str4));
System.out.println(str4.equalsIgnoreCase("APPLE"));
//[8]static String format(String ,Object ... obj )
int a = 10,b=2;
System.out.println(a+"/"+b+"="+(a/b));
System.out.println(String.format("%d/%d=%d", a, b,(a/b)));
}
}
String类中常用方法总结【一】
猜你喜欢
转载自blog.csdn.net/wyqwilliam/article/details/92738901
今日推荐
周排行