首页 技术 正文
技术 2022年11月15日
0 收藏 352 点赞 2,158 浏览 2183 个字

一、String常用的方法:

Java基础学习篇———String、集合的学习

1. == 实质比较两个对象的地址数值

String a = “hello”  (hello为匿名对象)

String a1 = “hello”

String b = new String(“hello”)

String b1 = new String(“hello”).intern

String c = b;

a == b ———-> false     false的原因:new会为b新开辟一块内存,所以比较时候两个地址不同(String里面含有intern方法)

                              可以手动向字符串池中添加,运行结果就是ture)

a == a1 ——–> ture       ture的原因:第一次字符串常量会放入“字符串池”中,下次再次申请时候,会指向同一块的内存

                                   如果不存在的话,会自动加入内存池中

b == c ———>true

注意:开发时候大部分使用String a1 = “hello”此类方法,避免new String对象会开辟新的内存,造成内存的的浪费。

                             即使new出新的对象,使用intern()方法也会把字符串加入字符串池中

2.equals : 字符串比较里面的内容

 b.equals(a)

 hello.equals(a)

如果String b = null 时候,系统会抛出nullPointExction(空指针异常),开发时候大部分都使用hello.equals(a)此类方法,

原因:就是可以避免出现字符串为空时候抛出的异常

3.开发过程中尽量不要大规模使用String + “hello” + “world”…………  , 因为这样也会造成内存的浪费

Java基础学习篇———String、集合的学习

4.String常用的函数名

 (1). char charAt(int index)  : 查找制定索引的字,第几个位置是什么字母就输出什么字母

 (2). boolean equals(String s)  : 比较两个字符的内容是否相同

 (3). boolean  equalsIgnoreCase(String s) : 比较两个字符串是否相同,忽略大小写

 (4). int compareTo(String s)  : 比较两个字符串的大小关系

 (5). boolean contains(String s) : 查找字符串是否存在

 (6). int indexOf(String s)   : 从起始位置开始查找字符串,存在返回索引数值,不存在返回-1

 (7). int indexOf(String s , int fromIndex )   :  从fromIndex位置开始查找,存在返回索引数值,不存在返回-1

 (8). boolean startWith(String s)   :  判断是否以指定字符串作为起始位置

 (9). boolean startWith(String s , int fromIndex)  :  判断是否已指定字符串从fromIndex位置作为起始位置

 (10). endWith(String s)  :  判断是否已指定字符作为结束位置

 (11). String replaceAll(String s , String replace)  :  把字符串s全部替换成replace字符串

 (12). String replaceFrist(String s , String replace) : 字符串replace只替换第一个s字符串,其余的s字符串的都不用替换

(13). String[]  split(String s)  :  把字符串s全部拆分成字符串数组          String  data[] = String.split(”  “) : 这个是按空格来拆分的

 (14). String  split(String s , int limit)  : 将字符串s部分拆分成字符串数组个数limit的长度

 (15). String  substring(int beginIndex)  :  从beginIndex位置截取到字符串结尾

 (16). String  substring(int beginIndex , int endIndex)  : 从beginIndex位置截取到字符串endIndex结束

 (17). String intern()  :  把字符串对象加入字符串池中

 (18). boolean isEmpty()  :  判断字符串是否为空

 (19). int length()  :   获得字符串的长度

 (20). String trim()  :  去掉字符串的左右空格

 (21). String  concat(String s)  : 字符串连接

 (22). String  toUpperCase()  :  将字符串小字母转化成大写字母

 (23). String  toLowerCase()  : 将字符串大写字母转成小写字母

5.字符串转为基本数据

  public static int  parseInt(String s)

  public static double  parseInt(String s)

  public static boolean  parseInt(String s)

6.基本数据类型转换成字符串

  public static String  valueOf(int 、double、boolean)

二、集合学习

  Java基础学习篇———String、集合的学习

  Java基础学习篇———String、集合的学习

  Java基础学习篇———String、集合的学习

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294