string.h java_Java String类

一,String类

java中用String类进行描述。对字符串进行了对象的封装。这样的好处是可以对字符串这种常见数据进行方便的操作。对象封装后,可以定义N多属性和行为。

1、字符串是一个特殊的对象。

2、字符串一旦初始化就不可以被改变。

字符串一旦被初始化,就不可以被改变,存放在方法区中的常量池中。

字符串的方法:

1:构造方法:将字节数组或者字符数组转成字符串。

String s1 = new String();//创建了一个空内容的字符串。

String s2 = null;//s2没有任何对象指向,是一个null常量值。

String s3 = "";//s3指向一个具体的字符串对象,只不过这个字符串中没有内容。

//一般在定义字符串时,不用new。

String s4 = new String("abc");

String s5 = "abc"; 一般用此写法

new String(char[]);//将字符数组转成字符串。

new String(char[],offset,count);//将字符数组中的一部分转成字符串。

2:一般方法:

按照面向对象的思想:

2.1获取:

2.1.1:获取字符串的长度。length();

2.1.2:指定位置的字符。char charAt(int index);

2.1.3:获取指定字符的位置。如果不存在返回-1,所以可以通过返回值-1来判断某一个字符不存在的情况。

int indexOf(int ch);//返回第一次找到的字符角标

int indexOf(int ch,int fromIndex); //返回从指定位置开始第一次找到的角标

int indexOf(String str); //返回第一次找到的字符串角标

int indexOf(String str,int fromIndex);

int lastIndexOf(int ch);

int lastIndexOf(int ch,int fromIndex);

int lastIndexOf(String str);

int lastIndexOf(String str,int fromIndex);

2.1.4:获取子串。

String substring(int start);//从start位开始,到length()-1为止.

String substring(int start,int end);//从start开始到end为止。//包含start位,不包含end位。

substring(0,str.length());//获取整串

2.2判断:

2.2.1:字符串中包含指定的字符串吗?

boolean contains(String substring);

2.2.2:字符串是否以指定字符串开头啊?

boolean startsWith(string);

2.2.3:字符串是否以指定字符串结尾啊?

boolean endsWith(string);

2.2.4:判断字符串是否相同

boolean equals(string);//覆盖了Object中的方法,判断字符串内容是否相同。

2.2.5:判断字符串内容是否相同,忽略大小写。

boolean equalsIgnoreCase(string) ;

2.3转换:

2.3.1:通过构造函数可以将字符数组或者字节数组转成字符串。

2.3.2:可以通过字符串中的静态方法,将字符数组转成字符串。

static String copyValueOf(char[] );

static String copyValueOf(char[],int offset,int count);

static String valueOf(char[]);

static String valueOf(char[],int offset,int count);

2.3.3:将基本数据类型或者对象转成字符串。

static String valueOf(char);

static String valueOf(boolean);

static String valueOf(double);

static String valueOf(float);

static String valueOf(int);

static String valueOf(long);

static String valueOf(Object);

2.3.4:将字符串转成大小写。

String toLowerCase();

String toUpperCase();

2.3.5:将字符串转成数组。

char[] toCharArray();//转成字符数组。

byte[] getBytes();//可以加入编码表。转成字节数组。

2.3.6:将字符串转成字符串数组。切割方法。

String[] split(分割的规则-字符串);

2.3.7:将字符串进行内容替换。注意:修改后变成新字符串,并不是将原字符串直接修改。

String replace(oldChar,newChar);

String replace(oldstring,newstring);

2.3.8: String concat(string); //对字符串进行追加。

String trim();//去除字符串两端的空格

int compareTo();//如果参数字符串等于此字符串,则返回值 0;如果此字符串按字典顺序小于字符串参数,则返回一个小于 0 的值;如果此字符串按字典顺序大于字符串参数,则返回一个大于 0 的值。

Demo:

1 packagecom.hpioneer.Demo;2

3 /**

4 * @Description: String类5 * @Author: HPioneer6 * @CreateTime: 2018/5/7 11:137 * @File: Java_String of JavaProject in com.hpioneer.Demo8 * @FullFileName: com.hpioneer.Demo.Java_String9 * @Create By IntelliJ10 * @Version: 1.011 */

12 public classDemo_String{13 public static voidmain(String[] args) {14

15 //字符数组转字符串

16 char[] Array = { 'r', 'u', 'n', 'o', 'o', 'b'};17 String string = newString(Array);18 System.out.println( string );19 System.out.println(string.length()); //长度

20 System.out.println("1、hello,".concat(string)); //拼接

21 System.out.println(2+ "、hello," + string); //拼接

22 System.out.printf("浮点型变量的值为 " +"%f, 整型变量的值为 " +" %d, 字符串变量的值为 " +"is %s" , 1.0, 1, "1.0");//格式化

23 System.out.println();24

25 String q = "a";26 String w = "b";27 String e = q +w;28 String r = new StringBuffer().append(q).append(w).toString(); //字符串相加的实现就是Buffer进行append之后tostring

29

30 String a = "saff";31 String b = "saff";32 String c = new String("saff");33 System.out.println(a.equals(b)); //true

34 System.out.println(a.equals(c)); //true

35 }36

37 }

二,StringBuffer类

构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。

特点:

1:可以对字符串内容进行修改。

2:是一个容器。

3:是可变长度的。

4:缓冲区中可以存储任意类型的数据。

5:最终需要变成字符串。

容器通常具备一些固定的方法:

1,添加。

StringBuffer append(data):在缓冲区中追加数据。追加到尾部。

StringBuffer insert(index,data):在指定位置插入数据。

2,删除。

StringBuffer delete(start,end);删除从start至end-1范围的元素

StringBuffer deleteCharAt(index);删除指定位置的元素

//sb.delete(0,sb.length());//清空缓冲区。

3,修改。

StringBuffer replace(start,end,string);将start至end-1替换成string

void setCharAt(index,char);替换指定位置的字符

void setLength(len);将原字符串置为指定长度的字符串

4,查找。(查不到返回-1)

int indexOf(string); 返回指定子字符串在此字符串中第一次出现处的索引。

int indexOf(string,int fromIndex);从指定位置开始查找字符串

int lastIndexOf(string); 返回指定子字符串在此字符串中最右边出现处的索引。

int lastIndexOf(string,int fromIndex); 从指定的索引开始反向搜索

5,获取子串。

string substring(start); 返回start到结尾的子串

string substring(start,end); 返回start至end-1的子串

6,反转。

StringBuffer reverse();字符串反转

Demo:

1 packagecom.hpioneer.Demo;2

3 /**

4 * @Description:5 * @Author: HPioneer6 * @CreateTime: 2018/5/7 11:587 * @File: Demo_StringBuffer of JavaProject in com.hpioneer.Demo8 * @FullFileName: com.hpioneer.Demo.Demo_StringBuffer9 * @Create By IntelliJ10 * @Version: 1.011 */

12 public classDemo_StringBuffer {13 public static voidmain(String[] args) {14

15 /*StringBuffer s = new StringBuffer();16 System.out.println(s.length());//017 System.out.println(s.capacity());//16 初始容量18

19 StringBuffer s1 = new StringBuffer(10);20 System.out.println(s1.length());//021 System.out.println(s1.capacity());//1022

23 StringBuffer s2 = new StringBuffer("asas");24 System.out.println(s2.length());//825 System.out.println(s2.capacity());//24 = 8+1626

27 System.out.println(s2.append(true));//asastrue28 s2.deleteCharAt(0);29 s2.delete(0,3);30 System.out.println(s2.append("dfdfdf"));//truedfdfdf31 System.out.println(s2.append(100));//truedfdfdf10032 System.out.println(s2.insert(4,"KK"));//trueKKdfdfdf10033 System.out.println(s2.reverse());//001fdfdfdKKeurt34 System.out.println(s2.replace(0,2,"111"));//1111fdfdfdKKeurt35 System.out.println(s2.substring(2,5));//11f*/

36

37 /*StringBuffer sb = new StringBuffer("heima");38

39 String s1 = new String(sb); //通过构造将StringBuffer转换为String40 System.out.println(s1);41

42 String s2 = sb.toString(); //通过toString方法将StringBuffer转换为String43 System.out.println(s2);44

45 String s3 = sb.substring(0, sb.length()); //通过截取子字符串将StringBuffer转换为String46 System.out.println(s3);*/

47

48 String s = "heima";49 System.out.println(s);50 change(s);51 System.out.println(s);52 //System.out.println(s+="itca");

53 System.out.println("---------------------");54 StringBuffer sb = newStringBuffer();55 sb.append("heima");56 System.out.println(sb);57 change(sb);58 System.out.println(sb);59

60

61 }62

63 public static voidchange(StringBuffer sb) {64 sb.append("itcast");65 }66

67 public static voidchange(String s) {68 s += "itcast";69 }70

71 }

3,StringBuilder

JDK1.5出现StringBuiler;构造一个其中不带字符的字符串生成器,初始容量为 16 个字符。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。

StringBuffer和 StringBuilder 的区别:

StringBuffer线程安全。

StringBuilder线程不安全。

单线程操作,使用StringBuilder 效率高。

多线程操作,使用StringBuffer 安全。

1 StringBuilder sb = new StringBuilder("abcdefg");2 sb.append("ak"); //abcdefgak

3 sb.insert(1,"et");//aetbcdefg

4 sb.deleteCharAt(2);//abdefg

5 sb.delete(2,4);//abefg

6 sb.setLength(4);//abcd

7 sb.setCharAt(0,'k');//kbcdefg

8 sb.replace(0,2,"hhhh");//hhhhcdefg9

10 //想要使用缓冲区,先要建立对象。

11 StringBuffer sb = newStringBuffer();12 sb.append(12).append("haha");//方法调用链。

13 String s = "abc"+4+'q';14 s = new StringBuffer().append("abc").append(4).append('q').toString();15 ---------------------------------------------------------

16 classTest{17 public static voidmain(String[] args) {18 String s1 = "java";19 String s2 = "hello";20 method_1(s1,s2);21 System.out.println(s1+"...."+s2); //java....hello

22

23 StringBuilder s11 = new StringBuilder("java");24 StringBuilder s22 = new StringBuilder("hello");25 method_2(s11,s22);26 System.out.println(s11+"-----"+s22); //javahello-----hello

27 }28 public static voidmethod_1(String s1,String s2){29 s1.replace('a','k');30 s1 =s2;31 }32 public static voidmethod_2(StringBuilder s1,StringBuilder s2){33 s1.append(s2);34 s1 =s2;35 }36 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值