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.分 ...
随机推荐
- Remote desktop cannot verify?教你如何应对
远程桌面:IIS7远程桌面IIS7远程桌面管理工具(3389.vps.服务器批量管理.批量远程工具)简介: 1.批量管理WIN系列服务器,VPS,电脑. 2.批量导入服务器的IP,端口,账号和密码 ...
- 将字符串按照一行N个内容展示,并保存到txt文档当中
str='丰东股份的 反倒是 发送到 电风扇发 的说法是 反倒是 aEQWW WERQR ERREW 34R32 ER 32432 32423 432142 234321 134214 32424 3 ...
- (Java实现) 洛谷 P1051 谁拿了最多奖学金
题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 院士奖学金,每人8000元,期末平均成绩高于80分(>80),并且在本学期内发表1篇或1篇以上 ...
- Java实现 LeetCode 48 旋转图像
48. 旋转图像 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示 ...
- Java实现九阶数独
你一定听说过"数独"游戏. 如[图1.png],玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个同色九宫内的数字均含1-9,不重复. 数独 ...
- centos6.5 安装 clickhouse
概述:clickhouse是一个高性能的列式数据库,特点就是快快快,查询性能是mysql的100-1000倍,非常适合存储频繁写入的数据,比如:日志,用户事件记录.单表存储上亿甚至十几亿行数据库查询都 ...
- Android开发之修改Manifest中meta-data的数据
代码 private void initFMMap() { ApplicationInfo appInfo = null; try { appInfo = this.getPackageManager ...
- https如何进行加密传输
客户端是没有证书的,也就没有公钥和私钥. SSL握手阶段,服务器把证书传输给客户端,同时也就传输了公钥(公钥是证书的一部分). 由客户端来对这个证书进行有效性认可,再由这个客户端来生成对称密钥. 对称 ...
- Windows下搭建Apache网站
目录 Apache下载 Apache安装 httpd.conf文件格式说明 启动服务并测试 Apache下载 在Apache官网底部找到APACHE PROJECT LIST里的HTTP Server ...
- linux安装QT-Designer两种方法
1.安装Qt-Creator, Qt-Creator自带了qt-designer 2.安装qt5-default,qttools5-dev-tools