String类基础知识
1、String类的构造方法
(1)String(String original) //把字符串数据封装成字符串对象
(2)String(char[] c) //把字符数组的数据封装成字符串对象
(3)String(char[] c, int index, int count) // 把字符数组中的一部分数据封装成字符串对象
示例:
public class Demo01 {
public static void main(String[] args) {
String str01=new String("hello");
char[] c1=new char[]{'h','e','l','l','o'};
String str02=new String(c1);
char[] c2=new char[]{'h','e','l','l','o','w','o','r','l','d'};
String str03=new String(c2,0,5);
System.out.println(str01);
System.out.println(str02);
System.out.println(str03);
}
}
输出位:
hello
hello
hello
2、String类常用方法
(1)public boolean equals(Object obj)
将此字符串与指定的对象比较。若内容相等,返回true,否则,返回false。
equal 和 == 的区别:
equal:比较的是值是否相同
==:比较的是地址值是否相同
示例:
public class demo01 {
public static void main(String[] args) {
String s1="helloworld";
String s2="hello";
String s3="world";
s2=s2+s3;
boolean aBoolean1=s1==s2;
boolean aBoolean2=s1.equals(s2);
System.out.println("s1:"+s1+" s2:"+s2);
System.out.println(aBoolean1);
System.out.println(aBoolean2);
}
}
输出结果为:
s1:helloworld s2:helloworld
false
true
(2)public boolean equalIgnoreCase(String otherString)
将此 String 与另一个 String 比较,不考虑大小写。如果两个字符串的长度相同,并且其中的相应字符都相等(忽略大小写),则认为这两个字符串是相等的。
示例:
public class demo02 {
public static void main(String[] args) {
String s1="hello world!";
String s2="Hello World!";
boolean aBoolean1=s1.equals(s2);
boolean aBoolean2=s1.equalsIgnoreCase(s2);
System.out.println("s1:"+s1+" s2:"+s2);
System.out.println("s1.equal(s2):"+aBoolean1);
System.out.println("s1.equalIgnoreCase(s2):"+aBoolean2);
}
}
输出结果为:
s1:hello world! s2:Hello World!
s1.equal(s2):false
s1.equalIgnoreCase(s2):true
(3)public boolean startsWith(String str)
判断字符串对象是否以指定的str开头
示例:
public class Demo04 {
public static void main(String[] args) {
String s="hello world!";
boolean b1=s.startsWith("helo");
boolean b2=s.startsWith("hell");
System.out.println(b1);
System.out.println(b2);
}
}
输出为:
false
true
(4)public boolean endsWith(String str)
判断字符串对象是否以指定的str结尾
示例:
public class Demo05 {
public static void main(String[] args) {
String s="hello world!";
boolean b1=s.endsWith("ld!");
boolean b2=s.endsWith("d!");
System.out.println(b1);
System.out.println(b2);
}
}
输出为:
true
true
(5)public int length()
获取字符串的长度,其实也就是字符个数
(6)public char charAt(int index)
获取指定索引处的字符
(7)public int indexOf(String str)
获取str在字符串对象中第一次出现的索引
示例:
public class Demo06 {
public static void main(String[] args) {
String str="hello world! this is me";
System.out.println("字符串\"hello world! this is me\"的长度为:"+str.length());
char c=str.charAt(2);
System.out.println("位于字符串str,索引为2的字符为:"+c);
int index=str.indexOf("ll");
System.out.println("\"ll\"在字符串str的索引位置是:"+index);
}
}
输出为:
字符串"hello world! this is me"的长度为:23
位于字符串str,索引为2的字符为:l
"ll"在字符串str的索引位置是:2
(8)public String substring(int start)
从start开始截取字符串
(9)public String substring(int start,int end)
从start开始,到end结束截取字符串。包括start,不包括end
(10)public char[] toCharArray()
把字符串转换为字符数组
(11)public String toLowerCase()
把字符串转换为小写字符串
(12)public String toUpperCase()
把字符串转换为大写字符串
(13)public String trim()
将去除字符串两端空格
(13)public String[] split (String str)
按照指定符号分割字符串
String类基础知识的更多相关文章
- 【C++】类-基础知识
类-基础知识 目录 类-基础知识 1. 语法定义 2. 类的实现 3. 三个基本的函数 3.1 构造函数 功能 形式 调用时机 默认构造函数 3.2 复制构造函数 功能 形式 调用时机 3.3 析构函 ...
- String 类相关知识
1.常用方法 1)判断字符串是否为空 public boolean isEmpty()2)获取字符串长度 public int length()3)截取子子串 public String substr ...
- Scanner、String(java基础知识十二)
1.Scanner的概述和方法介绍 * A:Scanner的概述 * 是一个从键盘输入的类,有final修饰,不能被子类继承 * Scanner sc = new Scanner(System.in) ...
- String类基础的那些事!
第三阶段 JAVA常见对象的学习 第一章 常见对象--String类 (一) String 类的概述及其构造方法 (1) 概述 多个字符组成的一串数据,例如 "abc" 也可以看成 ...
- Java String类相关知识梳理(含字符串常量池(String Pool)知识)
目录 1. String类是什么 1.1 定义 1.2 类结构 1.3 所在的包 2. String类的底层数据结构 3. 关于 intern() 方法(重点) 3.1 作用 3.2 字符串常量池(S ...
- java核心-多线程(4)-线程类基础知识
1.并发 <1>使用并发的一个重要原因是提高执行效率.由于I/O等情况阻塞,单个任务并不能充分利用CPU时间.所以在单处理器的机器上也应该使用并发. <2>为了实现并发,操作系 ...
- Python类基础知识(面向对象基础)
#首先 我们需要了解 面向过程是什么 面向对象是什么 我们为什么需要使用面向对象 面向过程:根据业务逻辑从上到下写垒代码 面向对象:根据代码对函数进行分类和封装 区别:解决问题的逻辑不同,但是都能解决 ...
- java中String类、StringBuilder类和StringBuffer类详解
本位转载自http://www.cnblogs.com/dolphin0520/p/3778589.html 版权声明如下: 作者:海子 出处:http://www.cnblogs.com/dolp ...
- 浅析String类
这是对于String类的一些总结,我将会从几个方面并且结合着字符串池等相关知识进行总结 进程如下: 1.对于String类基本知识的一些总结 2.简要介绍字符串池 3.分 ...
随机推荐
- 百度云百度网盘VIP不限速破解版绿色版-实测可用
百度云百度网盘不限速VIP破解版绿色版-下载地址:https://www.90pan.com/b1548999
- Docker容器同步主机时间
方法一: 查看本地是否有/etc/localtime文件 cat /etc/localtime 如果没有就新建文件 cp /usr/share/zoneinfo/Asia/Shanghai /et ...
- Java实现 蓝桥杯 算法提高 因式分解
算法提高 8-1因式分解 时间限制:10.0s 内存限制:256.0MB 提交此题 问题描述 设计算法,用户输入合数,程序输出若个素数的乘积.例如,输入6,输出23.输入20,输出22*5. 样例 与 ...
- 使用liunx系统自带的工具sar监控指定接口速率
1.路由器双出口部署,接口可以实现负载分担,在接口负载比例设置为1:2之后,管理员反馈流量有些异常,内网tracert -d 外网域名或者IP,都走一条链路. 2.底层尝试使用sar命令监控两个接口的 ...
- RabbitMQ(2)---高级使用
1.ack和限流 ack也就是消息确认签收,分为自动签收和手动签收.之前的交换机demo中:channel.basicConsume(queueName,true, consumer); 第二个参数 ...
- arduino 的analogRead() 和analogWrite()
模拟输入analogRead()函数的返回值范围是0 到1023; 而模拟输出analogWrite()函数的输出值范围是0 到255; 所以: val = analogRead(potpin); / ...
- [html][js]视频倍速播放功能
代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...
- 学习ASP.NET Core(10)-全局日志与xUnit系统测试
上一篇我们介绍了数据塑形,HATEOAS和内容协商,并在制器方法中完成了对应功能的添加:本章我们将介绍日志和测试相关的概念,并添加对应的功能 一.全局日志 在第一章介绍项目结构时,有提到.NET Co ...
- UI 小白设计常用的Website
网址 设计师导航网址(其实这个用好了,enough) 设计导航1 设计导航2 icon 阿里巴巴Iconfont flaticons icomoon Noun Project 图片(有时候就是灵魂) ...
- Serval and Parenthesis Sequence【思维】
Serval and Parenthesis Sequence 题目链接(点击) Serval soon said goodbye to Japari kindergarten, and began ...