当前位置: 首页 > news >正文

襄阳网站定制阜新小程序 阜新网站建设开发

襄阳网站定制,阜新小程序 阜新网站建设开发,西部数码网站管理助手 xp,中交路桥建设有限公司官网文章目录 String类的重要性常用的方法常用的构造方法String类的比较字符串的查找转化数字转化为字符串字符串转数字 字符串替换字符串的不可变性 字符串拆分字符串截取字符串修改 StringBuilder和StringBuffer String类的重要性 在c/c的学习中我们接触到了字符串#xff0c;但… 文章目录 String类的重要性常用的方法常用的构造方法String类的比较字符串的查找转化数字转化为字符串字符串转数字 字符串替换字符串的不可变性 字符串拆分字符串截取字符串修改 StringBuilder和StringBuffer String类的重要性 在c/c的学习中我们接触到了字符串但是在c中其实并没有字符串类型我们写的字符串是char arr[]这样子其实这个严格来说应该叫字符指针因此其实他并不是字符串类型而想要操作字符串需要用到库函数或者自己写的函数这并不符合面向对象的思想因此java中创造了String类。 常用的方法 常用的构造方法 常用的构造方法主要有三个 public static void main(){String arrhello;char[] a{h,e,l,l,o};String arr2new String(a);Strin arr3new String(arr); }常见的基本上就是以上三种第一种是直接使用等号第二种则是使用字符数组第三种直接利用已有的String对象new一个拷贝一个新的String对象。 此外要主要String是一种引用类型他的内部存储的是地址而不是字符串。这一点我们怎么证明呢请看下面的String类的比较 String类的比较 刚刚我们提到String类是引用类型它内部其实存储的是内存地址我们如何证明这个结论呢我们来看看下面一段代码 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {String strnew String(abcd);String str2new String(abcd);System.out.println((strstr2));} }这段代码打印出来的内容应该是什么呢我们分情况来讨论。 首先这个代码都是new一个相同内容的对象因此我们可以知道这两个对象内部存储的内容是相同的但是地址一定不同因此如果说String对象内部是直接存储的内容的话用等于判断结果应该是true否则如果是地址的话应该是false 而以上代码的最终结果也是false这其实就已经说明了他的内部存储的是地址而并非内容。而String内部的变量的关系如图 注意这里的内部实际关系中我们是可以看到一个String对象其内部还有一个value对象这个value对象指向内容。这里我们可以看一下源代码 可是我们在实际比较的过程中肯定是不希望比较的是地址而是希望以内容作为比较的那么这个问题该怎么解决呢其实String‘类的内部重写了equals方法我们在比较的时候其实使用这个方法进行比较的。 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {String strnew String(abcd);String str2new String(abcd);System.out.println((str.equals(str2)));} }而当我们使用equals进行比较时就可以发现最后的结果时正确的了但是我们会发现这样一个事情那就是当一个char[]类型和一个String类型的内容相同时返回的确实false这是为什么呢 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {String strnew String(abcd);String str2new String(abcd);char[] arr{a,b,c,d};System.out.println((str.equals(arr)));} }原因就要从equals的底层说起了我们来看一下equals的底层代码 我们看一下equals的底层就会发现这里呢会有两个判断首先第一个就是thisanObject以及anobject instanceof String这里是什么意思呢首先第一个if就是比较传进来的这个参数是不是自己本身那么这里我们传入的是一个char[]很明显第一个if进不去那么第二个if什么意思呢第二个if的意思就是传进来的对象是不是String类对象很明显也不是那么最终返回的肯定就是false了 字符串的查找 字符串的查找String中我们想要单独的拿出来一个字符无法像char[]那般直接进行[下标的方式]那么我们该如何呢字符串的查找String类用了一系列的方法其中最重要的一个是charAt()方法用法如下 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {String strnew String(abcd);for(int i0;istr.length();i){System.out.println(str.charAt(i));}} }他的使用和下标的使用是一样的。 转化 数字转化为字符串 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {String strnew String(abcd);String str2String.valueOf(10010);System.out.println(str2);} }字符串转数字 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {//String strnew String(abcd);String str2String.valueOf(10010);int aInteger.parseInt(str2);System.out.println(a);//System.out.println(str2);} }字符串替换 使用一个新的字符串替换掉原来的字符串这里我们要知道一个概念就是字符串的不可变性。 字符串的不可变性 字符串的不可变性是指字符串在创建之后字符串的内容事不能在被改变的换句话说字符串一旦呗创建他的内容就不能在被改变了任何一个尝试修改字符串的手段本质上来说都是用一个新的字符串替换 掉原有的字符串。 由此我们再来看替换字符串的两个函数我们也应当能够明白字符串的不可变性是的所有的替换其实都是不改变原有内容而是重新创建一个字符串 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {//String strnew String(abcd);String str2new String(abcd);//int aInteger.parseInt(str2);//System.out.println(a);//System.out.println(str2);String str3str2.replaceAll(a,p);System.out.println(str2);System.out.println(str3);} }因此我们可以看出来这个字符串中str2的内容是没有被改变的只是重新返回了一个字符串并赋值给了str3 字符串拆分 字符串的拆分是指将一个字符串中的某个字符作为分割字符从而将一个字符串拆分为多个子串。拆分的方法是split。我给大家演示一下 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {//String strnew String(abcd);String str2https:editor.csdn.netmd?not_checkout1spm1001.2014.3001.4503articleId136138248;String[] str3str2.split(\\.);System.out.println(str2);for(String s:str3){System.out.println(s);}} }这里是以.为分割符进行分割的。此外还有别的用法比如说你可以设定拆分几组 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {//String strnew String(abcd);String str2https:editor.csdn.netmd?not_checkout1spm1001.2014.3001.4503articleId136138248;String[] str3str2.split(\\.,2);System.out.println(str2);for(String s:str3){System.out.println(s);}} }此外我们还可以多次拆分 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {//String strnew String(abcd);String strzyficl,clizyf;String[] str2str.split(\\,);for(int i0;istr2.length;i){String[] str3str2[i].split();System.out.println(str3[0] str3[1]);}} }字符串截取 字符串截取一般都是用于我们想使用某个区间内的内容所需要的函数是String substring(int beginIndex) 他的用法如下 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {//String strnew String(abcd);String strzyficl,clizyf;String str2str.substring(2);System.out.println(str2);} }当只输入一个参数的时候它表示从改下标一直到结尾位置。 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {//String strnew String(abcd);String strzyficl,clizyf;String str2str.substring(2,3);System.out.println(str2);} }当输入两个参数的时候他表示的是一个左闭右开的区间内容正如实例中会输出f一样表示的是左闭右开 字符串修改 上面我们讲过字符串是不可变的任何对于字符串的修改严格来说都是重新创建了一个对象在进行修改这使得字符串的修改效率非常的慢因此我们一般都是不对字符串的内容做修改的可是在实际开发中肯定是会遇到一些时候是需要对字符串的内容做修改的地方的那么这时候怎么办呢 我们通常都是借助StringBuilder和StringBuffer StringBuilder和StringBuffer 在先讲StringBuilder和StringBuffer之前呢我们先说一下来看一下下面这个代码 public class Main {public static void main(String[] args) {//String strnew String(abcd)String strnew String();for(int i0;i100;i){stri;}System.out.println(str);} }这段代码就很简单就是每一次都在这个字符串后面拼接那么这个效率低嘛显而易见很低很低那么这是为什么呢因为我们上面说了因为他会创建新的对象那么创建新的对象创建的是什么对象呢其实是StringBuilder对象也就是说其实每一次 的改变都会创建一个StringBuilder对象来接受改变的字符串那么我们如果直接使用StringBuilder实例化出一个对象然后再不断调用append方法效率会不会持续提升呢很明显是肯定的。 // Press Shift twice to open the Search Everywhere dialog and type show whitespaces, // then press Enter. You can now see whitespace characters in your code. public class Main {public static void main(String[] args) {//String strnew String(abcd)String strnew String();StringBuilder stringBuildernew StringBuilder();for(int i0;i100;i){stringBuilder.append(i);}strstringBuilder.toString();System.out.println(str);} }因为我们刚刚讲了为什么效率低下因为总是创建新的对象那如何增高效率呢很明显直接降低实列化对象的次数就好了因此我们直接将StringBuilder定义出来并且直接使用StringBuilder去修改然后将StringBuilder赋值给String即可。 那么StringBuffer是什么呢 StringBuilder StringBuilder 是在 Java 5 中引入的它是非线程安全的。 因为不需要考虑线程安全问题所以 StringBuilder 的性能比 StringBuffer 更高。 StringBuilder 的方法不是线程安全的如果在多线程环境中使用可能会导致数据不一致或者其他问题。 StringBuffer StringBuffer 是在 Java 1.0 中引入的它是线程安全的。 由于需要考虑线程安全性StringBuffer 的性能通常比 StringBuilder 差一些。 StringBuffer 的方法是线程安全的可以在多线程环境中安全地使用。 在大多数情况下如果不需要考虑线程安全性推荐使用 StringBuilder因为它的性能更好。只有在需要在多线程环境中安全地操作字符串时才使用 StringBuffer。 学会向爱人妥协才能收获幸福的争吵少的生活你和爱人是要一起面对生活的而不是你们在互相较劲。
http://www.dnsts.com.cn/news/25782.html

相关文章:

  • 淘客怎么做自己的网站网站设计步骤及注意事项
  • 网站建设好处zu97自定义图片 wordpress
  • 云虚拟主机怎么建网站百度推广渠道代理
  • 绿色蔬菜网站模板网站空间有哪几种类型
  • 深圳最好的网站开发公司电话wordpress注册教程视频
  • 陕西网站建设软件盒子wordpress
  • 各种网站开发工具服务器租用
  • 河源网站搭建费用做交通招聘的网站
  • 英文网站开发公司网站建设公司简介范文
  • 深圳旅游网站开发深圳网站建设设计
  • myeclipse怎样做网站软件ui设计是什么
  • 云南建设厅网站安全处网站备案时间太长
  • 付公司网站费用怎么做分录深圳网站有哪些内容
  • o2o网站建设行业现状wordpress 友链插件
  • 上海网站建设lv cn建设酒店网站ppt模板
  • 旅游网站的市场需求怎么做介绍wordpress 功能 去除
  • 做浏览单的网站郑州做网站公司有多少
  • 学院网站建设wp网站如何做文件的付费下载
  • 偷网站源码直接建站医院网站怎么建设
  • 钰鸣厦门网站建设没有有知道钓鱼网站在哪儿做
  • 网站后台管理器怎么做怎样通过网络销售自己的产品
  • php网站开发技术代码call_user_func_array() wordpress
  • 网站上做扫一扫艾特思成都网站建设
  • 网站空间与服务器安徽省建设工程信息网招标公告
  • 临海网站开发公司电话公司网页下载
  • 中山seo网站优化公司wordpress修改文章点赞数
  • 模板网站能用吗拨号地址怎么做网站
  • 保安做网站关联网站有那些
  • 网站icp备案 技术负责人帝国cms7.0模板 绿色企业网站模板(整站带数据)
  • 广州增城网站建设wordpress小程序商城