一、String类
String非基本数据类型,但是同基本数据类型一样更为常用。
复制代码
结果是true false,证明str1和str都是创建了一个内容为空的字符串,但指向了两个不同的对象。其中
1
2
3
4
5
6
7
8public 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); } }
复制代码
是创建了一个String类型对象,并查询内存中是否已经存在“aaa”这异常量,如果存在直接引用,如果不存在则先创建再引用,这种模式也成为享元模式。
1String str=new 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() :忽略字符串前导空白和尾部空白。
一些字符串操作:
//编写一个可以获取文件扩展名的函数,形参接收一个文件名字符串,返回一个扩展名字符串。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public 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; }
//切割字符串 成字符串数组
复制代码
1
2
3
4
5
6
7public 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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12public 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()); } }
//判断字符串是否为镜面字符串
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public 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
StringBuffer和StringBuilder是可变长度的字符串缓冲区,最终还是要转为字符串操作。StringBuilder是非线程同步的,效率较高,StringBuffer线程同步。
常用方法
1.存储 StringBuffer append():将指定数据作为参数添加到已有数据结尾处。
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)
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)
复制代码
1
2
3
4
5
6public 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 }
追加,直接按照索引替换字符串或字符
复制代码
按索引清除字符或字符串
1
2
3
4
5
6
7
8
9public 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()); }
复制代码
在指定索引前插入字符串或字符串
1
2
3
4
5
6
7public static void main(String[] args) { StringBuffer buf = new StringBuffer("你好java"); buf.delete(1,3); buf.deleteCharAt(2); System.out.println(buf.toString()); }
复制代码
1
2
3
4
5
6
7
8public 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
byte Byte
short short
int Integer
long Long
boolean Boolean
float Float
double Double
char Character
包装类提供了与字符串之间的转换
基本数据类型转成字符串
包装类.toString(基本类型数据) 或者 “”+基本数据
如:
复制代码
1
2
3Boolean.toString(true);//""+true Integer.toString(40);""+40 Character.toString('A');""+A
字符串转基本数据类型
复制代码
1
2
3
4
5
6<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类也放在这里吧
处理特别大的数据时需要用到
BigInteger类
下面是计算1000的阶乘结果中有多少个0的例子
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13public 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); }
最后
以上就是光亮蛋挞最近收集整理的关于JAVA String及基本数据类型包装类的全部内容,更多相关JAVA内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复