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.分 ...
随机推荐
- js异步原理
javascript引擎 javascript引擎执行时单线程的,如果有一个任务被阻塞,整个浏览器就会处于假死状态 那么遇到这种情况,又在单线程的情况下,不能像java之类的语言创建多几个线程来处理, ...
- CSS3新增伪类有那些?
p:first-of-type 选择属于其父元素的首个元素 p:last-of-type 选择属于其父元素的最后元素 p:only-of-type 选择属于其父元素唯一的元素 p:only-child ...
- [统计信息系列7] Oracle 11g的自动统计信息收集
(一)统计信息收集概述 在Oracle 11g中,默认有3个自动任务,分别是:自动统计信息收集.SQL调优顾问.段空间调整顾问,查看方法如下: SQL> SELECT CLIENT_NAME,T ...
- Java实现 LeetCode 767 重构字符串(ASCII的转换)
767. 重构字符串 给定一个字符串S,检查是否能重新排布其中的字母,使得两相邻的字符不同. 若可行,输出任意可行的结果.若不可行,返回空字符串. 示例 1: 输入: S = "aab&qu ...
- Java实现 蓝桥杯 算法提高 摩尔斯电码
算法提高 9-3摩尔斯电码 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"zy ...
- Java实现 蓝桥杯 算法训练 数据交换
试题 算法训练 数据交换 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 编写一个程序,输入两个整数,分别存放在变量x和y当中,然后使用自己定义的函数swap来交换这两个变量的值. ...
- C#Winform中resx文件无效 找不到路径
问题由来 笔者因为更改了添加的图片的路径,再把路径改成图片所在的路径还是报resx文件无效,未能找到路径 问题原因 其实这个问题是因为对对象的引用修改了,但是resx文件中的应用还是没有修改.因为re ...
- Java实现 洛谷 P2141 珠心算测验
import java.util.LinkedList; import java.util.Scanner; public class Main { private static Scanner ci ...
- Java实现最大连续乘积子数组
1 问题描述 给定一个浮点数组,任意取出数组中的若干个连续的数相乘,请找出其中乘积最大的子数组. 2 解决方案 2.1 蛮力法 该方法的时间复杂度为O(n^2). package com.liuzhe ...
- 实用!看Python如何光速合并多个PDF
大家好,今天分享一个实用的办公脚本:将多个PDF合并为一个PDF, 例如我手上现在有如下3个PDF分册,需要整合成一个完整的PDF. 如果换成你操作的话,是不是打开百度搜索:PDF合并,然后去第三方网 ...