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.分布完成 声明数组:数据类型 数组名 [] = ...
随机推荐
- linux文件系统写过程简析
linux写入磁盘过程经历VFS -> 页缓存(page cache) -> 具体的文件系统(ext2/3/4.XFS.ReiserFS等) -> Block IO ->设备 ...
- OpenID Connect Core 1.0(八)从第三方发起登录
在某些情况下,登录流程由一个OpenID提供者或其他方发起,而不是依赖方(RP).在这种情况下,发起者重定向到RP在发起登录终结点,RP的请求验证请求发送到指定的OP.这个发起登录终结点可以在RP深度 ...
- Linux 下让终端走代理的方法
转载: https://blog.fazero.me/2015/09/15/%E8%AE%A9%E7%BB%88%E7%AB%AF%E8%B5%B0%E4%BB%A3%E7%90%86%E7%9A%8 ...
- CABasicAnimation使用总结
CABasicAnimation使用总结 实例化 使用方法animationWithKeyPath:对 CABasicAnimation进行实例化,并指定Layer的属性作为关键路径进行注册. //围 ...
- 1004. Counting Leaves(30)—PAT 甲级
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- Mysql数据库-DAY2
查找 Select 字段(全部查找为*)from 表 增加 Insert into 字段(全部增加为表) values(需要增加的内容,用'','隔开') 删除 Delete from 表 where ...
- VS Code 的常用快捷键及插件(前端)
一.vs code 的常用快捷键 1.注释: a) 单行注释:[ctrl+k,ctrl+c] 或 ctrl+/ b) 取消单行注释:[ctrl+k,ctrl+u] (按下ctrl不放,再按k + u) ...
- java 一个数字的位数不够怎么在前面加0
import java.text.DecimalFormat; //(1).如果数字1是字符串,如下处理: String str1="1"; DecimalFormat df=ne ...
- npm run build 时报错operation not permitted
1.项目使用vue框架,在npm run build 打包时报错: 访问对应的目录,发现无法打开,原来是文件被其他应用程序占用了,仔细看了一下,xftp文件传输的软件打开着,把它关闭以后,重新运行np ...
- ACM1012:u Calculate e
Problem Description A simple mathematical formula for e iswhere n is allowed to go to infinity. This ...