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.分布完成 声明数组:数据类型 数组名 [] = ...
随机推荐
- nRF5 SDK for Mesh(六) BLE MESH 的 基础概念
Basic Bluetooth Mesh concepts The Bluetooth Mesh is a profile specification developed and published ...
- 直接在apk中添加资源的研究
原文 http://blog.votzone.com/2018/05/12/apk-merge.html 之前接手过一个sdk的开发工作,在开发过程中有一个很重要的点就是尽量使用代码来创建控件,资源文 ...
- ibatis运行的SQL语句的输出——通过配置log4j
将ibatis 的log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句 ### 设置Logger输出级别和输出目的地 ###log4j.rootLogger=debug,st ...
- redefinition of class解决
垃圾玩意我在这儿翻车了. 编译器:Code::Block(懒得用VS,而且又太大了,CB小,而且也就一个控制台程序) Note to myself: 写完一个class的文件定义,编译,通过之后: 1 ...
- code#5 P1 报告
报告 时间限制: 1.0 秒 空间限制: 128 MB 相关文件: 题目目录 题目描述 企鹅高中有很多学生,自然管理起来也就非常麻烦.学校的教务处想要随时统计学校里面有多少个学生,但是他们只有很多 ...
- Ubuntu下apt方式安装与更新Git
本人使用的系统 Ubuntu 18.04.1 ,使用apt安装Git: sudo apt insatll git 安装后发现不是最新的版本,更新方法: sudo add-apt-repository ...
- Python实现创建字典
编写一个名为 make_album() 的函数,它创建一个描述音乐专辑的字典.1.这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典.使用这个函数创建三个表示不同专辑的字典,并打印每个返 ...
- MongoDB Linux安装
MongoDB 提供了 linux 各发行版本 64 位的安装包,你可以在官网下载安装包. 下载地址:https://www.mongodb.com/download-center#community ...
- Redis底层数据类型
Redis主要数据结构:简单动态字符串(SDS).双端链表.字典.跳跃表.整数集合.压缩列表和快速列表: 一.简单动态字符串(SDS): Redis没有直接使用C语言中的传统的字节数组保存字符串,而是 ...
- 我的职业规划(android)
通过一段时间的想法,自己大概圈定了自己的未来三年的职业规划,关于android的,希望大家多多批评,多多指教.或者大家也能讨论下自己对于未来的期许或者路线,虽然每个人都有自己自身的情况,但是总会有一些 ...