JAVA基础学习之路[2]String类常用方法

Posted 1996

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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基础学习之路定义简单java类

尚硅谷_Java零基础教程(常用类——String)-- 学习笔记

java学习之路--String类方法的应用

JAVA-基础-常用API(equals,tostring方法,String类)

我的java学习之路六:java的常用类

Java基础常用包装类