博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String类
阅读量:4106 次
发布时间:2019-05-25

本文共 5337 字,大约阅读时间需要 17 分钟。

String类

字符串的构造方法

  1. public String( ) :创建一个空白字符串,不含有任何内容。
  2. public String(char [ ] array) :根据字符数组的内容,来创建对应的字符串。
  3. public String(byte [ ] array) :根据字节数组的内容,来创建对应的字符串。
  4. 直接创建 String str = “HELLO”
public class Demo04String01 {
public static void main(String[] args) {
String str1 = new String(); System.out.println("第一个字符串是:"+str1); char [] charArray = {
'A','B','C'}; String str2 = new String(charArray); System.out.println("第二个字符串是:"+str2); byte [] byteArray = {
97,98,99}; String str3 = new String(charArray); System.out.println("第三个字符串是:"+str3); String str4 = "Hello"; System.out.println(str4); }}

注意:直接写上双引号,就是字符串对象。

字符串的常用比较方法

==是对字符串地址的比较,如果需要比较字符串的内容,可以使用两个方法:

public boolean equals (Object obj) : 参数可以是任何对象

public boolean equalsIgnoreCase (String str) :忽略大小写比较

注意事项

  1. 任何对象都能用Object进行接收
  2. equals方法具有对称性
  3. 如果比较常量和变量 推荐把常量字符串写在前 如: ABC.equals(str)
public class Demo04String02 {
public static void main(String[] args) {
String str1 = "ABC"; String str2 = "ABC"; char [] charArray = {
'A','B','C'}; String str3 = new String(charArray); System.out.println(str1.equals(str2));//true System.out.println(str1.equals(str3));//true System.out.println(str3.equals("ABC"));//true System.out.println("ABC".equals(str1));//true }}

字符串的获取相关方法

String中与获取相关的常用方法有:

public int length() :获取字符串当中含有的字符个数,拿到字符串长度。

public String concat(String str) :将当前字符串和和参数字符串拼接成为返回值新的字符串。

public char charAt (int index) :获取指定索引位置的单个字符。(索引从0开始)。

public int indexOf(String str) :查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回 -1 值。

public class Demo04String03 {
public static void main(String[] args) {
//获取字符串长度 int length = "asdsafgwfaesdf".length(); System.out.println(length); //拼接字符串 String str1 = "Hello"; String str2 = "World"; String str3 = str1.concat(str2); System.out.println(str3); //获取指定索引位置的单个字符 char c = "Hello".charAt(3); System.out.println(c); //查找参数字符串在本来字符串中第一次出现的索引位置 String benlai = "HelloWorld"; int llo = benlai.indexOf("llo"); System.out.println(llo); }}

字符串的截取方法

字符串的截取方法:

public String substring(int index):截取从参数位置一直到末尾,返回新字符串。

public String substring(int begin,int end):截取从begin到end结束中间的字符串。(左闭右开区间)

字符串的转换相关方法

public char[] toCharArray (): 将当前字符串拆分成为字符数组作为返回值。

public byye[] getBytes(): 获得当前字符串底层的字节数组。

public String replace(CharSequence oldString,charSequence newString):将所有出现的老字符串替换为新字符串。

package demo04;public class Demo04String04 {
public static void main(String[] args) {
char[] chars = "Hello".toCharArray();; System.out.println(chars[0]); System.out.println(chars.length); System.out.println("====================="); byte[] bytes = "abc".getBytes(); for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]); } System.out.println("====================="); String str = null; String str1 = "how do you do"; String o = str1.replace("o", "*"); System.out.println(o); System.out.println("======================"); String lang1 = "wdnmd,我带你们打"; String wdnmd = lang1.replace("wdnmd", "*****"); System.out.println(wdnmd); }}

字符串的分割方法

public String [] split(String regex) : 按照参数的规则,将字符串分为若干部分。

package demo04;public class Demo04String04 {
public static void main(String[] args) {
String str1 = "aaa,bbb,ccc"; String[] split = str1.split(","); for (int i = 0; i < split.length; i++) {
System.out.println(split[i]); } }}

注意事项

split方法的参数其实是一个正则表达式。 如果按照英文句点“.”进行切分,必须写 “\ \ .”

练习:按照指定格式拼接字符

题目:定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串。格式参照如下:[word1#word2#word3#]。

package demo04;public class Demo04String04 {
public static void main(String[] args) {
int [] array1 = {
1,2,3}; String array2 = fromAtoB(array1); System.out.println(array2); } public static String fromAtoB (int[] array){
String str = "["; for (int i = 0; i < array.length; i++) {
if (i == array.length-1){
str += "word"+array[i]+"]"; } else str += "word"+array[i]+"#"; } return str; }}

练习:统计输入的字符串中的种类

题目:键盘输入一个字符串,并且统计其中各种字符出现的次数。

种类有:大写字母、小写字母、数字、其他。

package demo04;import java.util.Scanner;public class Demo04String04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串:"); String input = sc.next(); int daxie = 0; int xiaoxie = 0; int shuzi = 0; int qita = 0; char[] chars = input.toCharArray(); for (int i = 0; i < chars.length; i++) {
char ch = chars[i]; if ('A' <= ch && ch <= 'Z') {
daxie++; } else if ('a' <= ch && ch <= 'z') {
xiaoxie++; } else if ('0' <= ch && ch <= '9') {
shuzi++; } else {
qita++; } } System.out.println("大写字母有:"+daxie+"个"); System.out.println("小写字母有:"+xiaoxie+"个"); System.out.println("数字有:"+shuzi+"个"); System.out.println("其他有:"+qita+"个"); }}

转载地址:http://jbjsi.baihongyu.com/

你可能感兴趣的文章
面试想拿 10K,HR 说我只配7k?
查看>>
那些人生“开挂”的程序员,都在干什么?
查看>>
影响科学圈的那些计算机代码
查看>>
乐视视频 App 图标改为“欠 122 亿”,网友:我在别家分红包,却在你家随份子!...
查看>>
为何程序员总喜欢写技术博客,看完恍然大悟...
查看>>
如何判断一家互联网公司要倒闭了?
查看>>
想快速上手机器学习?来看下这个 GitHub 项目!
查看>>
GitHub 标星 3.6k,一本开源的深度学习中文教程!
查看>>
9 款你不能错过的 JSON 工具
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
200页!分享珍藏很久的Python学习知识手册(附链接)
查看>>
4 岁小女孩给 Linux 内核贡献提交
查看>>
推荐几个私藏很久的技术公众号给大家
查看>>
王垠受邀面试阿里 P9,被 P10 面跪后网上怒发文,惨打 325 的 P10 赵海平回应了!...
查看>>
Python 趣味打怪:147 段简单代码助你从入门到大师
查看>>
卧槽!小姐姐用动画图解 Git 命令,这也太秀了吧?!
查看>>
厉害了!Python 编辑器界的神器 Jupyter ,推出官方可视化 Debug 工具!
查看>>
卧槽!Java 虚拟机竟然还有这些性能调优技巧...
查看>>
听说玩这些游戏能提升编程能力?
查看>>
7 年工作经验,面试官竟然还让我写算法题???
查看>>