一、String类
String非基本数据类型,但是同基本数据类型一样更为常用。
public class StringTest {
public static void main(String[] args) {
String str=new String("aaa");
String str1="aaa";
System.out.println(str.equals(str));
System.out.println(str==str1);
}
}
结果是true false,证明str1和str都是创建了一个内容为空的字符串,但指向了两个不同的对象。其中
String str=new String("aaa");
是创建了一个String类型对象,并查询内存中是否已经存在“aaa”这异常量,如果存在直接引用,如果不存在则先创建再引用,这种模式也成为享元模式。
String有多种构造方法,除了传入固定字符串外,还可以传入字节数组、字符数组、整形数组、StringBuffer、StringBuilder。同时可以传入编码方式,在处理字符流时比较常用,详见另一篇JAVA IO。
除了构造方法外,String类还提供了丰富的字符串处理方法,列举常见方法如下:
- char charAt(int index):取字符串中指定位置的字符,index从0开始计算。
- String concat(String str):连接字符串,等同于 “+”;
- boolean contentEquals(StringBuffer buffer):若二者包含的字符序列相同时就返回true;
- boolean equals(Object obj):将该字符串与指定对象比较,若二者包含的字符序列相等返回true;
- boolean equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写;
- byte[] getBytes():将该字符串转换成byte数组;
- int indexOf(String str):找出str字符串在该字符串中第一次出现的位置;
- int indexOf(String str, int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始;
- int lastIndexOf(String str) 返回指定子字符串在此字符串中最后一次出现处的索引;
- int length():返回当前字符串长度;
- String replace(char oldChar, char newChar) :返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
- String replaceAll(String regex, String replacement) 使用给定的 字符串replacement 替换此字符串所有的regex字符串;
- boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始.
- String[] split(String regex): 把字符串按指定的字符串分隔开。
- String substring(int beginIndex) 返回一个新的字符串,从beginIndex开始截取,它是此字符串的一个子字符串;
- String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串;[begin,end)
- char[] toCharArray() 将此字符串转换为一个新的字符数组。
- String toLowerCase() 将此 String 中的所有字符都转换为小写;
- String toUpperCase()转成大写;
- static String valueOf(基本类型 obj):把基本类型值转成字符串;
- String trim() :忽略字符串前导空白和尾部空白。
public static void main(String[] args) {
System.out.println(getExtendName("aaa.java"));
}
/**
* @param name_file 传入String类型文件名
* @return String类型扩展名 无扩展名则返回null
*/
public static String getExtendName(String name_file) {
// TODO Auto-generated method stub
String name_extend = null;
if (name_file.indexOf('.') != -1)
name_extend = name_file.substring(name_file.lastIndexOf('.') + 1);
return name_extend;
}
//切割字符串 成字符串数组
public static void main(String[] args) {
String strs="aaa,bbb,ccc,ddd";
String[] str=strs.split(",");
for (String string : str) {
System.out.println(string);
}
}
//大小写转换 trim replace
public static void main(String[] args) {
String strs=" aaa, bbb,ccc,ddd ";
strs=strs.replace(",", "-");
strs=strs.toUpperCase();
String[] str=strs.split("-");
for (String string : str) {
System.out.println(string.trim());
}
}
//判断字符串是否为镜面字符串
public static void main(String[] args) throws Exception {
System.out.println("请输入任意字符串");
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in,"UTF-8"));
String str;
while ((str=reader.readLine())!=null&&!"stop".equals(str)) {
System.out.println(isMirror(str));
}
reader.close();
}
public static boolean isMirror(String str){
boolean mirror = true;
for (int x=0;x<str.length()/2;x++ ){
//str.length()/2取字符串一半长度的上整,判断到该位置即结束
if(str.charAt(x)!=str.charAt(str.length()-x-1)){
mirror = false;
break;
}
}
return mirror;
}
StringBuffer和StringBuilder是可变长度的字符串缓冲区,最终还是要转为字符串操作。StringBuilder是非线程同步的,效率较高,StringBuffer线程同步。
StringBuffer insert(index,数据):可以将数据插入到指定index位置。
2.删除 StringBuffer delete(start,end):删除缓冲区中的数据,包含start,不包含end。
StringBuffer deleteCharAt(index):删除指定位置的字符。
3.获取 char charAt(int index)
int indexOf(String str)
int lastIndexOf(String str)
int length()
String substring(int start, int end)
4.修改 void setCharAt(int index, char ch) ;
5.反转 StringBuffer reverse();
6.将缓冲区中指定数据存储到指定字符数组中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
public static void main(String[] args) {
StringBuilder buf = new StringBuilder("abcdef");
char[] chs = new char[6];
//sb.getChars(srcBegin, srcEnd, dst, dstBegin);
buf.getChars(1, 4, chs, 0); //第chs的0个位置开始,将“abcdef”的元素从位置1开始到位置4结束但不包括位置4复制给chs
}
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("你好java");
buf.replace(0,2,"黑马");
buf.setCharAt(2,'J');
buf.append("程序员");
System.out.println(buf.toString());
}
按索引清除字符或字符串
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("你好java");
buf.delete(1,3);
buf.deleteCharAt(2);
System.out.println(buf.toString());
}
在指定索引前插入字符串或字符串
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("你好java");
buf.insert(2,'黑');
buf.insert(3,'马');
buf.insert(4, "程序员");
System.out.println(buf.toString());
}
三、包装类
byte Byte
short short
int Integer
long Long
boolean Boolean
float Float
double Double
char Character
Boolean.toString(true);//""+true
Integer.toString(40);""+40
Character.toString('A');""+A
字符串转基本数据类型
<span style="white-space:pre"> </span>int num1 = Integer.parseInt("123");
double num2 = Double.parseDouble("2.71828");
boolean istrue = Boolean.parseBoolean("true");
Integer num3 = new Integer("123");
int num = num3.intValue();
刚刚接触到了一个BigInteger类也放在这里吧
public static void main(String[] args) {
BigInteger bigNum = new BigInteger("1");
for (int i = 1; i <= 1000; i++) {
bigNum = bigNum.multiply(new BigInteger("" + i));
}
int num = 0;
for (int i = 0; i < bigNum.toString().length(); i++) {
char ch = bigNum.toString().charAt(i);
if (ch == '0')
num++;
}
System.out.println(num);
}
199



被折叠的 条评论
为什么被折叠?



