黑马程序员技术交流社区
标题:
【记录】代码练习-String类
[打印本页]
作者:
Kevin.Kang
时间:
2015-7-6 14:20
标题:
【记录】代码练习-String类
package com.kxg_04;
public class StringDemo {
public static void main(String[] args) {
String s = "helloworld";
String s2 = "HelloWorld";
String s3 = "HELLOWORLD";
String s4 = "helloworld";
String s5 = "hello";
String s6 = "";
// String s7 = null;
// A:判断功能
// boolean equals(Object obj):比较字符串内容是否相同,区分大小写
System.out.println(s.equals(s4));
// boolean equalsIgnoreCase(String str):比较字符串内容是否相同,不区分大小写
System.out.println(s.equalsIgnoreCase(s2));
// boolean contains(String str):判断大字符串中是否包含小字符串
System.out.println(s.contains(s5));
// boolean startsWith(String str):判断字符串是否以指定字符串开头
System.out.println(s.startsWith(s5));
// boolean endsWith(String str):判断字符串是否以指定字符串结尾
System.out.println(s.endsWith("ld"));
// boolean isEmpty():判断字符串是否为空
System.out.println(s6.isEmpty());
// System.out.println(s7.isEmpty());//s7对象指向空,空指针异常
System.out.println("===================");
// B:获取功能
// int length():获取字符串长度
System.out.println(s.length());
// char charAt(int index):获取指定索引位置的字符
System.out.println(s.charAt(5));
// int indexOf(int ch):获取指定字符在字符串中的索引
System.out.println(s.indexOf("l"));
// int indexOf(String str):获取定子字符串在此字符串中第一次出现处的索引
System.out.println(s.indexOf("l"));
// int indexOf(int ch,int fromIndex):获取指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
System.out.println(s.indexOf("l", 3));
// int indexOf(String str,int
// fromIndex):获取指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
System.out.println(s.indexOf("l", 3));
// String substring(int start):从指定索引截取字符串,默认到结尾
System.out.println(s.substring(6));
// String substring(int start,int end):从指定位置截取字符串到指定位置结束
System.out.println(s.substring(4, 7));
System.out.println("===================");
// C:转换功能
// byte[] getBytes():字符串转换为字节数组
byte[] by = s.getBytes();
for (int x = 0; x < by.length; x++) {
System.out.println(by[x]);
}
// char[] toCharArray():字符串转换为字符数组
char[] str = s.toCharArray();
for (int x = 0; x < str.length; x++) {
System.out.println(str[x]);
}
// static String valueOf(char[] chs):字节数组转换为字符串
System.out.println(String.valueOf(str));
// static String valueOf(int i):int类型的数据转成字符串
int i = 48;
String str2 = String.valueOf(i);
System.out.println(str2);
// String toLowerCase():字符串转换为小写
System.out.println(s3.toLowerCase());
// String toUpperCase():字符串转换为大写
System.out.println(s.toUpperCase());
// String concat(String str):字符串拼接
System.out.println(s.concat(s5));
System.out.println("===================");
// D:其他功能
// a:替换功能
// String replace(char old,char new)
System.out.println(s.replace("l", "k"));
// String replace(String old,String new)
System.out.println(s.replace("ll", "kkk"));
// b:去空格功能
// String trim()
String sk = " helloworld ";
System.out.println(sk.trim());
// c:按字典比较功能
// int compareTo(String str)
System.out.println(s.compareTo(s4));
System.out.println(s.compareTo(s5));
// int compareToIgnoreCase(String str)
}
}
复制代码
作者:
life木子飞
时间:
2015-7-6 15:50
顶一下 {:3_57:}
作者:
Kevin.Kang
时间:
2015-7-6 16:56
package com.kxg_01;
public class StringBufferDemo {
public static void main(String[] args) {
// StringBuffer的构造方法
// StringBuffer的方法
// public int capacity()返回缓冲区容量
// A:StringBuffer():无参构造
StringBuffer sb = new StringBuffer();
System.out.println(sb);
System.out.println(sb.capacity());
System.out.println(sb.length());
// B:StringBuffer(int size):指定容量的字符串缓冲区对象
StringBuffer sb2 = new StringBuffer(50);
System.out.println(sb2);
System.out.println(sb2.capacity());
System.out.println(sb2.length());
// C:StringBuffer(String str):指定字符内容的字符串缓冲区内存对象
StringBuffer sb3 = new StringBuffer("helloworld");
System.out.println(sb3);
System.out.println(sb3.capacity());
System.out.println(sb3.length());
// A:添加功能
// public StringBuffer append(String str):把任意类型元素添加到字符串缓冲区中,并返回本身
sb.append("sdfsdf").append(12313).append(12.332);
System.out.println(sb);
// public StringBuffer insert(int offset,String
// str):指定位置插入任意类型元素到字符串缓冲区,并返回本身
sb.insert(5, "xiao");
System.out.println(sb);
// B:删除功能
// public StringBuffer deleteChatAt(int index):删除指定位置的字符,并返回本身
sb.deleteCharAt(6);
System.out.println(sb);
// public StringBuffer deleteCharAt(int start, int
// end):删除指定位置到结束位置,并返回本身
sb.delete(6, 12);
System.out.println(sb);
sb.delete(10, sb.length());
System.out.println(sb);
// C:替换功能
// public StringBuffer replace(int start,int end,String
// str):从指定位置到结尾用指定字符串代替
sb.replace(2, 5, "xiaoxiaoxiao");
System.out.println(sb);
// D:反转功能
// public StringBuffer reverse():把字符串反转
sb.reverse();
System.out.println(sb);
// E:截取功能
// public String substring(int start):指定位置截取,默认到结尾,返回String类型
// public String substring(int start,int end):指定位置到指定结尾,返回String类型
String s = sb.substring(2);
String s2 = sb.substring(2, 5);
System.out.println(s);
System.out.println(s2);
System.out.println(sb);
}
}
复制代码
作者:
qihuan
时间:
2015-7-6 21:30
顶
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2