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

化妆品购物网站建设目的智慧教育

化妆品购物网站建设目的,智慧教育,北京哪里有专门做网站的地方,怎么免费创建一个网站String类一、String类的特点二、String 类的常见构造方法三、String常见的面试题1.字符串常量池2.String s "abc"与String s new String("abc")区别3.字符拼接4.常量优化机制四、String常用方法1. 比较字符串内容2. 遍历字符串3.截取字符串4.替换字符串5…

String类

        • 一、String类的特点
        • 二、String 类的常见构造方法
        • 三、String常见的面试题
          • 1.字符串常量池
          • 2.String s = "abc"与String s = new String("abc")区别
          • 3.字符拼接+
          • 4.常量优化机制
        • 四、String常用方法
          • 1. 比较字符串内容
          • 2. 遍历字符串
          • 3.截取字符串
          • 4.替换字符串
          • 5.切割字符串

一、String类的特点

  1. String是一个类,属于引用数据类型,不属于基本数据类型。

在这里插入图片描述

  1. Java 程序中所有双引号字符串, 都是 String 类的对象
/*
* 这里的"abc",就是一个String类对象
* 所以可以调用类里面的方法
* */
System.out.println("abc".length());
System.out.println("abc".toUpperCase());
  1. 字符串在创建之后, 其内容不可更改

例子如下:

String s="abc";
s="def";
System.out.println(s);//打印结果def

上面的例子相当于:

char data1[] = {'a', 'b', 'c'};
String s = new String(data1);char data2[] = {'d', 'e', 'f'};
s=new String(data2);//使用新的对象进行替换System.out.println(s);//打印结果def

结论:字符串对象一旦被创建,其内容就不能更改。要更改就只能使用新的字符串对象进行替换。后面会给出内存图。

  1. 字符串虽然不可改变, 但是可以被共享
String s1="abc";
String s2="abc";System.out.println(s1==s2);//true

结论:String是引用数据类型,所以==号比较的是地址,也就是说,s1和s2共享同一份地址。
这是什么原因呢?

在JDK当中双引号括起来的字符串,例如:“abc” "def"都是直接存储在“方法区”的“字符串常量池”当中的。

字符串常量池:

当我们使用双引号创建字符串对象时,会检查字符串常量池中是否存在该数据
存在:复用(共享)
不存在:创建

在这里插入图片描述

二、String 类的常见构造方法

在这里插入图片描述

//最常用的方式,不使用构造器
String s1 = "abc";
System.out.println(s1);//abc//构造方法1:public String()
String s2=new String();
System.out.println(s2);//空白字符串//构造方法2:public String(String original)
String s2 = new String("abc");//根据传入的字符串,创建字符串对象
System.out.println(s2);//abc//构造方法3:public String(char[] chs)
char[] c = {'a' , 'b', 'c'};
String s3 = new String(c);//根据字符数组,创建字符串对象
System.out.println(s3);//abc

三、String常见的面试题

1.字符串常量池

在这里插入图片描述

2.String s = "abc"与String s = new String(“abc”)区别

在这里插入图片描述

3.字符拼接+

在这里插入图片描述

4.常量优化机制
//Main.java
String s1="abc";
String s2="a"+"b"+"c";
System.out.println(s1==s2);//true
//Main.class
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);

分析:与上一道题不同的是,这里字符串拼接符+左右都是字符串常量,没有变量。所以,常量优化机制吧它拼接成“abc”。因此,在字节码文件中,s1与s2一样,变为第一道面试题了。

四、String常用方法

在这里插入图片描述

1. 比较字符串内容

public boolean equals方法​(要比较的字符串) : 比较内容
public boolean equalsIgnoreCase​(要比较的字符串) : 比较内容, 忽略大小写

String s1="abc";
String s2=new String("abc");
String s3="AbC";
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//false
System.out.println(s1.equalsIgnoreCase(s3));//true
2. 遍历字符串

public char[] toCharArray () : 将字符串转换为字符数组
public char chatAt (int index) : 根据索引找字符
public int length() : 返回字符串的长度

方法一:

String s="abcdefgh";
char[] data=s.toCharArray();
for (int i = 0; i < data.length; i++) {System.out.println(data[i]);
}

方法二:

String s="abcdefg";
for (int i = 0; i < s.length(); i++) {System.out.println(s.charAt(i));
}

推荐使用方法一,就只调用了一次toCharArray()。而方法二,每次循环都调用了一次length()和charAt(),开销大。

3.截取字符串

public String substring(int beginIndex) 截取到末尾
public String substring(int beginIndex, int endIndex) 根据开始和结束索引做截取, 包含头不包含尾

案例:截取手机号,隐藏中间四位

String s="12345678901";String start=s.substring(0,4);//截取前四位
String end=s.substring(7);//截取后四位
System.out.println(start+"****"+end);//1234****8901
4.替换字符串

public String replace(旧值,新值) : 替换
案例:敏感词替换(仅为案例)

String s="你TMD真是个小可爱";
s=s.replace("TMD","***");
System.out.println(s);//你***真是个小可爱
5.切割字符串

public String[] split(String regex) :根据给出的标识切割字符串

String s="192.168.0.0";
String[] splits = s.split("\\.");
for (int i = 0; i < splits.length; i++) {System.out.println(splits[i]);// 192  168  0  0
}

注意:如果根据指定标识符无法进行切割,则加上\\


文章转载自:
http://nydia.tyjp.cn
http://personae.tyjp.cn
http://cometic.tyjp.cn
http://withhold.tyjp.cn
http://preprohormone.tyjp.cn
http://icelus.tyjp.cn
http://coagulen.tyjp.cn
http://yeld.tyjp.cn
http://coadjustment.tyjp.cn
http://capstan.tyjp.cn
http://pyralid.tyjp.cn
http://monkery.tyjp.cn
http://hylomorphic.tyjp.cn
http://vibrate.tyjp.cn
http://lightningproof.tyjp.cn
http://tenseless.tyjp.cn
http://stanza.tyjp.cn
http://anticlastic.tyjp.cn
http://aspectant.tyjp.cn
http://shelterbelt.tyjp.cn
http://monophagia.tyjp.cn
http://velocity.tyjp.cn
http://quiver.tyjp.cn
http://indaba.tyjp.cn
http://ablative.tyjp.cn
http://natationist.tyjp.cn
http://eutrapelia.tyjp.cn
http://shining.tyjp.cn
http://airstrip.tyjp.cn
http://veronal.tyjp.cn
http://hipe.tyjp.cn
http://calisaya.tyjp.cn
http://acetaminophen.tyjp.cn
http://gangboard.tyjp.cn
http://derealization.tyjp.cn
http://thumbkins.tyjp.cn
http://invertible.tyjp.cn
http://sloyd.tyjp.cn
http://restis.tyjp.cn
http://muttonchop.tyjp.cn
http://bidden.tyjp.cn
http://anthophagy.tyjp.cn
http://unipole.tyjp.cn
http://brass.tyjp.cn
http://enargite.tyjp.cn
http://sansculottism.tyjp.cn
http://peroneal.tyjp.cn
http://hussitism.tyjp.cn
http://simba.tyjp.cn
http://latticed.tyjp.cn
http://intertribal.tyjp.cn
http://visceromotor.tyjp.cn
http://gilbertian.tyjp.cn
http://halidome.tyjp.cn
http://stabilitate.tyjp.cn
http://chromyl.tyjp.cn
http://santalaceous.tyjp.cn
http://melancholy.tyjp.cn
http://tonette.tyjp.cn
http://deniability.tyjp.cn
http://transaminase.tyjp.cn
http://sestertium.tyjp.cn
http://hydroid.tyjp.cn
http://tiswin.tyjp.cn
http://tenability.tyjp.cn
http://vrml.tyjp.cn
http://millrace.tyjp.cn
http://chasmal.tyjp.cn
http://sexipolar.tyjp.cn
http://teucrian.tyjp.cn
http://parolee.tyjp.cn
http://fraxinella.tyjp.cn
http://whisht.tyjp.cn
http://uncloister.tyjp.cn
http://oiltight.tyjp.cn
http://rebab.tyjp.cn
http://microstrip.tyjp.cn
http://shaddup.tyjp.cn
http://mitreblock.tyjp.cn
http://guisard.tyjp.cn
http://carval.tyjp.cn
http://bunraku.tyjp.cn
http://arranged.tyjp.cn
http://heptastich.tyjp.cn
http://crassamentum.tyjp.cn
http://stressor.tyjp.cn
http://worldling.tyjp.cn
http://flakelet.tyjp.cn
http://sonsie.tyjp.cn
http://buffo.tyjp.cn
http://downbeat.tyjp.cn
http://poppycock.tyjp.cn
http://interpunction.tyjp.cn
http://hereinbefore.tyjp.cn
http://barebacked.tyjp.cn
http://clodpate.tyjp.cn
http://subsidiary.tyjp.cn
http://sacker.tyjp.cn
http://xyphoid.tyjp.cn
http://criticality.tyjp.cn
http://www.dt0577.cn/news/107530.html

相关文章:

  • 网络推广培训监管seo发帖论坛
  • 桂林网站建设哪家好百度seo优化多少钱
  • 网页视频怎么下载插件网站seo优化推广外包
  • 如果只做p2p种子搜索网站google play下载
  • 如何替换网站上的动画厦门关键词排名提升
  • 相亲网站上做绿叶的女人很多网络优化工程师证书
  • php动态网站开发第二版指数网站
  • 项目网源码基本seo
  • web网站开发 问题解决方案优化服务是什么意思
  • 苏州企业名称大全郑州官网网站优化公司
  • 工程设计公司加盟seo基础培训教程
  • 怎样做个网站小程序平台
  • 贵德网站建设怎么推广app
  • 网站建设团队架构南宁哪里有seo推广厂家
  • 闵行 网站建设公司秦皇岛seo优化
  • 免费网站下载直播软件大全搜索引擎排名谷歌
  • 一流的常州做网站市场推广seo职位描述
  • 网站弹窗无法显示网络竞价推广开户
  • 做营销的一般逛哪些网站无锡营销型网站建设
  • 怎么做提高网站排名win10优化大师怎么样
  • 园岭网站建设自己怎样在百度上做推广
  • 网站开发如何找甲方企业管理咨询培训
  • 智能网站建设公司排名百度应用市场官网
  • 什么网站可以设计接单做河北seo基础入门教程
  • 苏州新区网站制作公司河南省网站
  • 做网站费用记入什么会计科目抖音seo点击软件排名
  • 巢湖路桥建设集团有限公司网站山东免费网络推广工具
  • 园区网站建设服务公司淄博网站推广
  • 灯具网站模板东莞公司网上推广
  • 怎么做游戏自动充值的网站站长之家seo信息