字符与字符串:

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类常用方法的更多相关文章

  1. JAVA基础学习之路(三)类定义及构造方法

    类的定义及使用 一,类的定义 class Book {//定义一个类 int price;//定义一个属性 int num; public static int getMonney(int price ...

  2. Java基础系列2:深入理解String类

    Java基础系列2:深入理解String类 String是Java中最为常用的数据类型之一,也是面试中比较常被问到的基础知识点,本篇就聊聊Java中的String.主要包括如下的五个内容: Strin ...

  3. JAVA基础学习之路(八)[1]String类的基本特点

    String类的两种定义方式: 直接赋值 通过构造方法赋值 //直接赋值 public class test2 { public static void main(String args[]) { S ...

  4. JAVA基础学习之路(一)基本概念及运算符

    JAVA基础概念: PATH: path属于操作系统的属性,是系统用来搜寻可执行文件的路径 CALSSPATH: java程序解释类文件时加载文件的路径 注释: 单行注释  // 多行注释 /*... ...

  5. JAVA基础学习之路(十一)引用传递

    引用传递: 不同栈内存可以指向同一块堆内存,不同栈内存可以对一块堆内存进行修改 范例一: class Message { private int num = 10; public Message(in ...

  6. Java基础篇(02):特殊的String类,和相关扩展API

    本文源码:GitHub·点这里 || GitEE·点这里 一.String类简介 1.基础简介 字符串是一个特殊的数据类型,属于引用类型.String类在Java中使用关键字final修饰,所以这个类 ...

  7. Java基础学习笔记十九 IO

    File IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再 ...

  8. Java基础学习笔记十九 File

    IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再把这些数据 ...

  9. JAVA基础学习之路(五)数组的定义及使用

    什么是数组:就是一堆相同类型的数据放一堆(一组相关变量的集合) 定义语法: 1.声明并开辟数组 数据类型 数组名[] = new 数据类型[长度]: 2.分布完成 声明数组:数据类型 数组名 [] = ...

随机推荐

  1. SQL 语句 merge into

    MERGE INTO tb_st_shxxcount tt USING ( SELECT DISTINCT sd.CODE, COUNT (ts.LRDW) count1, TO_CHAR (ts.L ...

  2. 跟我一起学Linux-线程创建,类似FreeRTOS创建任务

    1.参考学习大神网址:http://blog.csdn.net/ithomer/article/details/6063067 #include<stdio.h> #include< ...

  3. SharePoint2010QuickFlow安装及使用

    一:QuickFlow的安装 1,从http://quickflow.codeplex.com/下载解决方案包以及设计器. 2,将QuickFlow.dll以及QuickFlow.UI.dll添加到程 ...

  4. oracle与infomix异同点

    之前是做oracle数据库应用开发的,现在工作用的是informix,特别不习惯.用了一段时间后才慢慢适应,最近做系统升级,把informix换成oracle数据库.顺便整理了一下informix与o ...

  5. vue面试题!!!

    由于公司需要,需要把项目拆分,前端使用vue框架.最近面试vue总结的试题 1:mvvm框架是什么?它和其他框架的区别是什么? mvvm 全称model view viewModel,model数据模 ...

  6. 一个百度MAP导航的基础封装

    项目中需要根据点击时候点击的内容,输入百度地图查找并展示规划等相关功能 于是封装了一个单独的百度map的html页面以供调用 功能包括了 ①展示底图 ②切换卫星图,切换卫星路线图,切换普通地图 ③通过 ...

  7. 怎样在Win7系统中搭建Web服务器

    一.搭建web服务 1.打开控制面板,选择并进入“程序”,双击“打开或关闭Windows服务”,在弹出的窗口中选择“Internet信息服务”下面所有的选项,点击确定后,开始更新服务. 2.更新完成后 ...

  8. echarts 报错问题 is null 或者未定义等问题

    我们在使用echarts的时候会出现is null或者未定义等报错提示,但是却无从下手的情况. 其一,我们是完全按照echarts的官方文档来添加的js文件:其二,在对使用option时候的配置是按照 ...

  9. python列表的通用操作

    #'+'和'*'#+可以将两个列表拼接为一个列表my_list = [1,2,3]+[4,5,6]#*可以将列表重复指定的次数my_list = [1,2,3]*5 print(my_list) #创 ...

  10. Python3 图像识别(二)

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 以图搜图的使用已经非常广泛了,我现在来介绍一下简单的以图搜图的相关算法及其实践. 一.感知hash算法 感知哈希算 ...