String类获取功能

来源:互联网 发布:java 日志重定向 编辑:程序博客网 时间:2024/06/05 20:44
/*
 *  String类获取功能
 */
public class StringTestAcquire{
public static void main(String[] args) {
method_9();
}

/*
* String类的获取方法
* public String substring(int begin)
* 获取字符串中的一部分 返回新的字符串
* 从指定索引开始,后面的全要
*/
public static void method_9(){
String s = "helloworld";
s = s.substring(1);
System.out.println(s);
}

/*
* String类的获取方法
* public String substring(int begin,int end)
* 获取字符串中的一部分 返回新的字符串
* 开始下标和结束下标,包含头,不包含尾
*/
public static void method_8(){
String s = "helloworld";
//获取字符串的一部分,截取 2,6
s = s.substring(2,6);
System.out.println(s);
}


/*
* String类获取方法
* public int length()
* 获取字符串中字符的个数,也就是字符串的长度

* length 数组长度,属性
* length()  字符串长度,方法
*/
public static void method_7(){
String s = "wqdfghjkuty5r4234trghfbvfdeqtryhjgnbdfeqrt6rgfsdewrtrf";
//调用String类的方法length()获取长度
int length = s.length();
System.out.println(length);
}


/*
* String类的获取方法
* public int lastIndexOf(char ch,int formIndex)
* 找指定字符在字符串中最后一次出现的索引,从指定下标处开始找,反向查找
*/
public static void method_6(){
String s = "HelloWorld";
//找l 字符,在字符串s中最后一次出现的索引,从5下标开始
int index = s.lastIndexOf('l',3);
System.out.println(index);
}


/*
* String类的获取方法
* public int lastIndexOf(char ch)
* 找指定字符在字符串中最后一次出现的索引,反向查找
*/
public static void method_5(){
String s = "System.println";
//找t 在字符串s中最后一次出现的索引
int index = s.lastIndexOf('t');
System.out.println(index);
}


/*
* String类的获取方法
* public int indexOf(String s,int formIndex)
* 找一个字符串在另一个字符串中第一次出现的索引,从指定的索引开始找
*/
public static void method_4(){
String s = "System.out.println";
//找字符串out,在字符串s中第一次出现的索引,从10下标开始找
int index = s.indexOf("out",8);
System.out.println(index);
}


/*
* String类的获取方法
* public int indexOf(String s)
* 找一个字符串在另一个字符串中第一次出现的索引
*/
public static void method_3(){
String s = "System.out.println";
//找字符串out在字符串s中第一次出现的索引
int index = s.indexOf("out");
System.out.println(index);
}


/*
* String类的获取方法
* public int indexOf(char ch,int formIndex)
* 获取一个字符,在一个字符串中第一次出现的索引,从指定的下标开始查找
*/
public static void method_2(){
String s = "System.out.println()";
//找字符t 在字符串中第一次出现的索引,指定开始索引从5开始
int index = s.indexOf('t',5);
System.out.println(index);
}


/*
* String类的获取方法
* public int indexOf(char ch)
* 获取一个字符,在一个字符串中第一次出现的索引
* 找不到返回负数
* 通用,获取内容:如果找的是基本类型,找不到返回负数
* 如果找的引用类型数据,找不到返回null
*/
public static void method_1(){
String s = "how are you";
//字符串中,找字符e 第一次出现的索引
int index  = s.indexOf('e');
System.out.println(index);
}


/*
* String类的获取方法
* public char charAt(int index)
* 获取指定索引出的单个字符
* "abcd"
*/
public static void method(){
String s = "abcdef";
//获取指定索引上的字符,3
char c = s.charAt(3);
System.out.println(c);
}
}
0 0
原创粉丝点击