java中的string函数_Java中关于String的常用函数

一、 构造方法:

* public String():创建String对象

*    public String(byte[] bytes):把字节数组转成字符串。

*    public String(byte[] bytes,int index,int length):把字节数组中的一部分转成字符串

*    public String(char[] value):把字符数组转成字符串

*    public String(char[] value,int index,int count):把字符数组的一部分转成字符串

* public String(String original):把字符串转成字符串

注意问题:

* 1:输出语句输出任何对象名称的时候,默认调用的是该对象的toString()方法。

*   而toString()方法默认输出的是包名...类名@哈希值的十六进制。

* 如果,你用输出语句输出一个对象名称的时候,发现不是这个格式,说明了该类重写了toString()方法。

* 2:返回此字符串的长度

* public int length()

*

二、String s = new String("hello")和String s ="hello"; 的区别:

==:比较的是引用类型,比较的是地址值即:

System.out.println(s1 == s2); // false

equal():默认比较的是地址值。String类重写了equals()方法,该方法的作用是比较字符串的内容是否相同

System.out.println(s1.equals(s2)); // true

三、 * 字符串变量相加:先开空间,再加内容

* 字符串常量相加:先加,再找,没有再开空间

String s1 = "hello";

String s2 = "world";

String s3 = "helloworld";

String s4 = s1 + s2;

String s5 = "hello"+"world";

即s4和s5的区别。

四、String类的判断功能:

* boolean equals(Object obj):比较字符串的内容是否相同,严格区分大小写

* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,不考虑大小写

* boolean contains(String str):判断是否包含指定的小串

* boolean startsWith(String str):判断是否以指定的字符串开头

* boolean endsWith(String str):判断是否以指定的字符串结尾

* boolean isEmpty():判断字符串的内容是否为空

五、String类的获取功能:

* int length():返回字符串的长度。字符的个数。

* char charAt(int index):返回字符串中指定位置的字符。

* int indexOf(int ch):返回指定字符在字符串中第一次出现的位置

* int indexOf(String str):返回指定字符串在字符串中第一次出现的位置

* int indexOf(int ch,int fromIndex):返回指定字符从指定位置开始在字符串中第一次出现的位置

* int indexOf(String str,int fromIndex):返回指定字符串从指定位置开始在字符串中第一次出现的位置

* String substring(int start):返回从指定位置开始到末尾的子串

* String substring(int start,int end):返回从指定位置开始到指定位置结束的子串----注意左包右不包

六、String的转换功能:

* byte[] getBytes():把字符串转换为字节数组

* char[] toCharArray():把字符串转换为字符数组

* static String valueOf(char[] chs):把字符数组转成字符串

* static String valueOf(int i):把int类型的数据转成字符串

* 把任意类型转换为字符串的方法。

* String toLowerCase():把字符串转小写

* String toUpperCase():把字符串转大写

* String concat(String str):字符串的连接

替换功能:

*    String replace(char old,char new)

*    String replace(String old,String new)

去除字符串两空格:

*      String trim()

按字典顺序比较两个字符串  a-z

* int compareTo(String str)

* int compareToIgnoreCase(String str)

---------------------

作者:qq9764312

来源:CSDN

原文:https://blog.csdn.net/qq9764312/article/details/79563031

版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值