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.分 ... 
随机推荐
- 编程-Byte order & Bit order
			https://mp.weixin.qq.com/s/B9rKps4YsLiDTBkRks8rmQ 看到比特序和字节序放在一起被提及,想必就已经填补了概念拼图里面缺失的那一块了,这一块正是比特序. 一 ... 
- jchdl - GSL实例 - MulC2(有符号数的乘法)
			这里的实现,先把符号位取出来,使用两个正数相乘,然后在把符号加到乘积上. 参考链接 https://github.com/wjcdx/jchdl/blob/master/src/org/jch ... 
- treegrid树形表格的完美运用
			一 问题描述: 树形表格TreeGrid在日常项目中还是运用的比较多的,哪我们在项目中,应该怎么引入和使用 TreeGrid呢? 二 使用步骤 1.首先我们需要在项目中,引入TreeGrid组件 需 ... 
- Java实现 蓝桥杯 算法训练VIP  报数(暴力+数学)约瑟夫环问题
			试题 算法训练 报数 问题描述 现有n个同学站成一圈,顺时针编号1至n.从1号同学开始顺时针1/2报数,报到1的同学留在原地,报到2的同学退出圆圈,直到只剩一名同学为止.问最后剩下的同学编号. 输入格 ... 
- Java实现 蓝桥杯VIP 算法提高 去注释
			算法提高 去注释 时间限制:1.0s 内存限制:256.0MB 去注释 问题 给你一段C++代码,将其中的注释去除后输出剩余的代码. 注释共有两种形式: 1. 行注视:以//开头,一直作用到行尾为止. ... 
- Java实现旅行商问题
			1 问题描述 何为旅行商问题?按照非专业的说法,这个问题要求找出一条n个给定的城市间的最短路径,使我们在回到触发的城市之前,对每个城市都只访问一次.这样该问题就可以表述为求一个图的最短哈密顿回路的问题 ... 
- java实现 洛谷 P1014 Cantor表
			题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 - 2/1 2/2 2/3 2/4 - ... 
- java实现第七届蓝桥杯棋子换位
			棋子换位 棋子换位 有n个棋子A,n个棋子B,在棋盘上排成一行. 它们中间隔着一个空位,用"."表示,比如: AAA.BBB 现在需要所有的A棋子和B棋子交换位置. 移动棋子的规则 ... 
- k8s学习-文档&概念
			1.文档大全 kubernetes objects文档(yaml文件编写): https://kubernetes.io/docs/concepts/overview/working-with-obj ... 
- eclipse中testNG的两种安装方式
			今天给大家带来两种关于testNG中的安装方式:1.在线安装(本人亲测有效!!!)2.离线安装 一.在线安装testNG插件的步骤: 1.给大家提供一个testNG在线的安装的地址:http://dl ... 
