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.分 ...
随机推荐
- 【Java Spring Cloud 实战之路】- 使用Nacos和网关中心的创建
0. 前言 在上一节中,我们创建了一个项目架构,后续的项目都会在那个架构上做补充. 1. Nacos 1.1 简介 Nacos可以用来发现.配置和管理微服务.提供了一组简单易用的特性集,可以快速实现动 ...
- Java实现 LeetCode 621 任务调度器(暴力大法)
621. 任务调度器 给定一个用字符数组表示的 CPU 需要执行的任务列表.其中包含使用大写的 A - Z 字母表示的26 种不同种类的任务.任务可以以任意顺序执行,并且每个任务都可以在 1 个单位时 ...
- Java实现第十届蓝桥杯迷宫
试题 E: 迷宫 本题总分:15 分 [问题描述] 下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方. 010000 000100 001001 110000 迷 ...
- Java实现第九届蓝桥杯递增三元组
题目6.递增三元组 题目描述 给定三个整数数组 A = [A1, A2, - AN], B = [B1, B2, - BN], C = [C1, C2, - CN], 请你统计有多少个三元组(i, j ...
- Linux 终止进程
kill命令 kill -l,查看可用的进程号 支持64种,常用的三种,1代表重启,9代表强制终止,15代表正常终止 kill -1 进程ID号,重启进程 killall -9 进程名,杀死对应的全部 ...
- c/c++混编
/* head.h */#ifndef __SUM_H__ #define __SUM_H__ #ifdef __cplusplus extern "C" { #endif int ...
- python自学Day05(自学书籍python编程从入门到实践)
第6章 字典 6.1 一个简单的字典 先跟随书本创建一个简单的字典感受一下. alien_0 = {'color':'green','points':5} print(alien_0['color'] ...
- Java基础(十)
一.XML概述 属性文件是用来描述程序配置,属性文件包含了一组名/值对.属性文件采用的是一种单一的平面层次结构,同时属性文件要求键是唯一的. XML格式能够表达层次结构,并且重复的元素不会被曲解. H ...
- 计算从1到n中,出现某位数字的次数
出现1-9中某位数字次数的算法 /** * @param input 整数n(1 ≤ n ≤ 1,000,000,000) * @return 1-9中某个数字在数列中出现的次数 */ public ...
- 程序员实用JDK小工具归纳,工作用得到
在JDK的安用装目录bin下,有一些有非常实用的小工具,可用于分析JVM初始配置.内存溢出异常等问题,我们接下来将对些常用的工具进行一些说明. JDK小工具简介 在JDK的bin目录下面有一些小工具, ...