2016年1月16日 关于String类…
2016-03-15 21:29
295 查看
public class StringAIPIDemo01 {
public static void main(String args[]){
String str="hello";//通过直接赋值的方式
char c=str.charAt(1);//取出字符串中指定位置的字符
System.out.println(c);
}
}
运行结果
h
将字符串转换为字符数组
将全部的字符数组转换为字符串
public class StringAIPIDemo02 {
public static void main(String args[]){
String str="hello world !!";//通过直接赋值的方式
char c[]=str.toCharArray();//将字符串转换为字符数组
for(int i=0;i
System.out.print(c[i]+"|");
}
String str1=new String(c);//将全部的字符数组转换为字符串
String str2=new String(c,0,5);//将0~5的字符数组转换为字符串
System.out.println("\n"+str1);
System.out.println(str2);
}
}
运行结果:
h|e|l|l|o| |w|o|r|l|d| |!|!|
hello world !!
hello
将全部字符串转换为byte数组
public class StringAIPIDemo03 {
public static void main(String args[]){
String str="hello world !!";//通过直接赋值的方式
byte b[]=str.getBytes();//将全部字符串转换为byte数组
String str1=new String(b);//将全部的byte数组转换为字符串
String str2=new String(b,0,5);//将0~5的byte数组转换为字符串
System.out.println(str1);
System.out.println(str2);
}
}
判断开头结尾是否为指定的字符
public class StringAIPIDemo04 {
public static void main(String args[]){
String str="** hello world ##";//通过直接赋值的方式
System.out.println(str.endsWith("##"));//判断是否以##结尾
System.out.println(str.startsWith("**"));//判断是否以**开头
}
}
运行结果:
true
true
替换操作
public class StringAIPIDemo05 {
public static void main(String args[]){
String str="** hello world ##";//通过直接赋值的方式
String Newstr=str.replaceAll("l", "e");//将l换为e
System.out.println(Newstr);
}
}
截取操作
public class StringAIPIDemo06 {
public static void main(String args[]){
String str="hello world !";//通过直接赋值的方式
String sub1=str.substring(6);
String sub2=str.substring(0,5);
System.out.println(sub1);
System.out.println(sub2);
}
}
运算结果
world !
hello
//这里需要注意的是第一种截取是从6开始截取,而第二种是从0截取到5.如下图所示
hello world !!
0123456789
拆分字符串
public class StringAIPIDemo07 {
public static void main(String args[]){
String str="hello world!";//通过直接赋值的方式
String s[]=str.split(" ");//拆分空格两边的
for(String st:s){
System.out.println(st);
}
}
}
运行结果:
hello
world!
查找操作:
public class StringAIPIDemo08 {
public static void main(String args[]){
String str="hello world!";//通过直接赋值的方式
System.out.println(str.contains("hello"));
System.out.println(str.contains("ohh"));
}
}
运行结果:
true
false
第二种方法查找(更加常用!)
public class StringAIPIDemo09 {
public static void main(String args[]){
String str="hello world!";//通过直接赋值的方式
System.out.println(str.indexOf("hello"));
System.out.println(str.indexOf("ohh"));
if(str.indexOf("ohh")!=-1){
System.out.println("查到所需内容");
}else{
System.out.println("未查到!");
}
}
}
运行结果:
0
-1
未查到!
查找时还可以规定从第几位找
public class StringAIPIDemo10 {
public static void main(String args[]){
String str="hello world!";//通过直接赋值的方式
System.out.println(str.indexOf("h"));
System.out.println(str.indexOf("h",5));//从第6未开始查找
if(str.indexOf("o")!=-1){
System.out.println("查到所需内容");
}else{
System.out.println("未查到!");
}
}
}
运行结果:
0
-1
查到所需内容
public static void main(String args[]){
String str="hello";//通过直接赋值的方式
char c=str.charAt(1);//取出字符串中指定位置的字符
System.out.println(c);
}
}
运行结果
h
将字符串转换为字符数组
将全部的字符数组转换为字符串
public class StringAIPIDemo02 {
public static void main(String args[]){
String str="hello world !!";//通过直接赋值的方式
char c[]=str.toCharArray();//将字符串转换为字符数组
for(int i=0;i
System.out.print(c[i]+"|");
}
String str1=new String(c);//将全部的字符数组转换为字符串
String str2=new String(c,0,5);//将0~5的字符数组转换为字符串
System.out.println("\n"+str1);
System.out.println(str2);
}
}
运行结果:
h|e|l|l|o| |w|o|r|l|d| |!|!|
hello world !!
hello
将全部字符串转换为byte数组
public class StringAIPIDemo03 {
public static void main(String args[]){
String str="hello world !!";//通过直接赋值的方式
byte b[]=str.getBytes();//将全部字符串转换为byte数组
String str1=new String(b);//将全部的byte数组转换为字符串
String str2=new String(b,0,5);//将0~5的byte数组转换为字符串
System.out.println(str1);
System.out.println(str2);
}
}
判断开头结尾是否为指定的字符
public class StringAIPIDemo04 {
public static void main(String args[]){
String str="** hello world ##";//通过直接赋值的方式
System.out.println(str.endsWith("##"));//判断是否以##结尾
System.out.println(str.startsWith("**"));//判断是否以**开头
}
}
运行结果:
true
true
替换操作
public class StringAIPIDemo05 {
public static void main(String args[]){
String str="** hello world ##";//通过直接赋值的方式
String Newstr=str.replaceAll("l", "e");//将l换为e
System.out.println(Newstr);
}
}
截取操作
public class StringAIPIDemo06 {
public static void main(String args[]){
String str="hello world !";//通过直接赋值的方式
String sub1=str.substring(6);
String sub2=str.substring(0,5);
System.out.println(sub1);
System.out.println(sub2);
}
}
运算结果
world !
hello
//这里需要注意的是第一种截取是从6开始截取,而第二种是从0截取到5.如下图所示
hello world !!
0123456789
拆分字符串
public class StringAIPIDemo07 {
public static void main(String args[]){
String str="hello world!";//通过直接赋值的方式
String s[]=str.split(" ");//拆分空格两边的
for(String st:s){
System.out.println(st);
}
}
}
运行结果:
hello
world!
查找操作:
public class StringAIPIDemo08 {
public static void main(String args[]){
String str="hello world!";//通过直接赋值的方式
System.out.println(str.contains("hello"));
System.out.println(str.contains("ohh"));
}
}
运行结果:
true
false
第二种方法查找(更加常用!)
public class StringAIPIDemo09 {
public static void main(String args[]){
String str="hello world!";//通过直接赋值的方式
System.out.println(str.indexOf("hello"));
System.out.println(str.indexOf("ohh"));
if(str.indexOf("ohh")!=-1){
System.out.println("查到所需内容");
}else{
System.out.println("未查到!");
}
}
}
运行结果:
0
-1
未查到!
查找时还可以规定从第几位找
public class StringAIPIDemo10 {
public static void main(String args[]){
String str="hello world!";//通过直接赋值的方式
System.out.println(str.indexOf("h"));
System.out.println(str.indexOf("h",5));//从第6未开始查找
if(str.indexOf("o")!=-1){
System.out.println("查到所需内容");
}else{
System.out.println("未查到!");
}
}
}
运行结果:
0
-1
查到所需内容
相关文章推荐
- 2016年1月16日 关于String类…
- 2016年1月18日 String练习题
- 2016年1月18日 15:31:19关于引用传…
- 2016年1月18日 19:07:22人与书的类
- 2016年1月18日 21:47:46关于this_2…
- 2016年1月19日 static声明属…
- 2016年1月20日 代码块的类别
- 2016-01-20 java中的链表设计…
- 2016年1月22日 关于继承(重点)
- 2016-01-23
- 2016-01-27 关于抽象类
- 2016-01-29 接口实现的方法
- 2016-02-01
- 2016-02-01_2  Object类
- 2016-02-01_3 关于匿名内部类
- 2016-02-01_4  包装类
- 2016-02-01 异常处理
- 2016-02-04  泛型的产生
- 蓝桥杯 基础训练02 字串
- 蓝桥杯2016-02-11  字母…