JAVA基础学习之路(九)[2]String类常用方法
字符与字符串:
1.将字符数组变为字符串(构造方法)
public String(char[] value)
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.
Parameters:
value - The initial value of the string
2.将部分字符数组变为字符串(构造方法)
public String(char[] value,
int offset,
int count)
Allocates a new String that contains characters from a subarray of the character array argument. The offset argument is the index of the first character of the subarray and the count argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string.
Parameters:
value - Array that is the source of characters
offset - The initial offset
count - The length
Throws:
IndexOutOfBoundsException - If the offset and count arguments index characters outside the bounds of the value array
3.返回指定索引对应的字符(普通方法)
charAt(int index)
Returns the char value at the specified index.
public class test1 {
public static void main(String args[]) {
String str = "hello";
char c = str.charAt(1);
System.out.println(c);
}
}
//e
4.将字符串变为字符数组(普通方法)
toCharArray()
Converts this string to a new character array.
public class test1 {
public static void main(String args[]) {
String str = "hello";
char [] data = str.toCharArray();
for(int i=0;i<data.length;i++) {
data[i]-=32;//转大写
System.out.print(data[i]+",");
}
System.out.println(new String(data));//将字符数组转化为字符串
}
}
字节与字符串
1。将字节数组转换为字符串(构造方法)
String(byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform's default charset.
2.将字节数组的一部分转换为字符串(构造方法)
String(byte[] bytes, int offset, int length)
Constructs a new String by decoding the specified subarray of bytes using the platform's default charset.
3.将字符串变为字节数据(普通)
public byte[] getBytes()
Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required. Returns:
The resultant byte array
Since:
JDK1.1
4.用于编码转换(普通)
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
The behavior of this method when this string cannot be encoded in the given charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required. Parameters:
charsetName - The name of a supported charset
Returns:
The resultant byte array
Throws:
UnsupportedEncodingException - If the named charset is not supported
Since:
JDK1.1
字符串比较判断
1.equals(普通),进行相等判断,区分大小写
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Overrides:
equals in class Object
Parameters:
anObject - The object to compare this String against
Returns:
true if the given object represents a String equivalent to this string, false otherwise
See Also:
compareTo(String), equalsIgnoreCase(String)
2.进行相等判断,不区分大小写
public boolean equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.
Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true: The two characters are the same (as compared by the == operator)
Applying the method Character.toUpperCase(char) to each character produces the same result
Applying the method Character.toLowerCase(char) to each character produces the same result
Parameters:
anotherString - The String to compare this String against
Returns:
true if the argument is not null and it represents an equivalent String ignoring case; false otherwise
See Also:
equals(Object)
3.判断两个字符大小,(按照字符编码比较)
public int compareTo(String anotherString)
Specified by:
compareTo in interface Comparable<String>
Parameters:
anotherString - the String to be compared.
Returns:
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.
compareToIgnoreCase
字符串查找
1.
JAVA基础学习之路(九)[2]String类常用方法的更多相关文章
- JAVA基础学习之路(三)类定义及构造方法
类的定义及使用 一,类的定义 class Book {//定义一个类 int price;//定义一个属性 int num; public static int getMonney(int price ...
- Java基础系列2:深入理解String类
Java基础系列2:深入理解String类 String是Java中最为常用的数据类型之一,也是面试中比较常被问到的基础知识点,本篇就聊聊Java中的String.主要包括如下的五个内容: Strin ...
- JAVA基础学习之路(八)[1]String类的基本特点
String类的两种定义方式: 直接赋值 通过构造方法赋值 //直接赋值 public class test2 { public static void main(String args[]) { S ...
- JAVA基础学习之路(一)基本概念及运算符
JAVA基础概念: PATH: path属于操作系统的属性,是系统用来搜寻可执行文件的路径 CALSSPATH: java程序解释类文件时加载文件的路径 注释: 单行注释 // 多行注释 /*... ...
- JAVA基础学习之路(十一)引用传递
引用传递: 不同栈内存可以指向同一块堆内存,不同栈内存可以对一块堆内存进行修改 范例一: class Message { private int num = 10; public Message(in ...
- Java基础篇(02):特殊的String类,和相关扩展API
本文源码:GitHub·点这里 || GitEE·点这里 一.String类简介 1.基础简介 字符串是一个特殊的数据类型,属于引用类型.String类在Java中使用关键字final修饰,所以这个类 ...
- Java基础学习笔记十九 IO
File IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再 ...
- Java基础学习笔记十九 File
IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再把这些数据 ...
- JAVA基础学习之路(五)数组的定义及使用
什么是数组:就是一堆相同类型的数据放一堆(一组相关变量的集合) 定义语法: 1.声明并开辟数组 数据类型 数组名[] = new 数据类型[长度]: 2.分布完成 声明数组:数据类型 数组名 [] = ...
随机推荐
- Angular动态表单生成(一)
好久不写博客了,手都生了,趁着最近老大让我研究动态表单生成的时机,撸一发博客~~ 开源项目比较 老大丢给我了两个比较不错的开源的动态表单生成工具,这两个项目在github上的star数量基本持平: h ...
- 使用supervior 监控 elasticsearch 进程
elasticsearch引擎在使用中可能会出现后台守护进程挂掉的情况,需要手动启动来恢复正常. 这时则可以引用supervior进程管理工具来监控elasticsearch进程状态,实现进程挂掉自动 ...
- 为什么我们要做三份 Webpack 配置文件
时至今日,Webpack 已经成为前端工程必备的基础工具之一,不仅被广泛用于前端工程发布前的打包,还在开发中担当本地前端资源服务器(assets server).模块热更新(hot module re ...
- CUBE,ROLLUP 和 GROUPING
1.用 CUBE 汇总数据 CUBE 运算符生成的结果集是多维数据集.多维数据集是事实数据的扩展,事实数据即记录个别事件的数据.扩展建立在用户打算分析的列上.这些列被称为维.多维数据集是一个结果集,其 ...
- android学习:关于RelativeLayout叠放布局的问题
RelativeLayout布局关于元素叠加的问题 1.RelativeLayout布局中的元素如果要实现元素叠加必须设置 RelativeLayout.ALIGN_PARENT_TOP 这样元素 ...
- 在express中HMR(合并express和webpack-dev-server)
在学习react的时候,经常用create-react-app来创建web应用,然而写到后面总有连自己服务器和数据库的需求,create-react-app创建的是一个webpack-dev-serv ...
- LR--用栈实现移进--归约分析(demo)
1.考虑文法 \(E->E+E\) \(E->E*E\) \(E->id\) 2.最右推导 不难看出,这个文法是而二义的,所以有多个最右推导 3.移进归约 用一个栈存文法符号,用输入 ...
- es6 入坑笔记(三)---数组,对象扩展
数组扩展 循环 arr.foreach(){ //回调函数 function(val,index,arr){ //val:当前读取到的数组的值,index:当前读取道德数组的索引,arr:当前的数组名 ...
- Java使用多线程实现Socket多客户端的通信
要想详细了解socket,大家请自行百度,我这里只简单介绍. 在网络中,我们可以利用ip地址+协议+端口号唯一标示网络中的一个进程.而socket编程就是为了完成两个唯一进程之间的通信(一个是客户端, ...
- 将Windows 7安装到移动固态硬盘(U盘)
由于工作的原因,有时需要用两台电脑工作,而搞开发的人大多又是追求计算机性能的人,所以笔记本电脑自然不轻,更何况两台. 两台电脑折磨我半年多,终于下定决心将个系统安装到移动固态硬盘中,背一台无硬盘电脑加 ...