1.Arrays.copyOf()
Arrays的copyOf()方法传回的数组是新的数组对象,改变传回数组中的元素值,不会影响原来的数组。
copyOf()的第二个自变量指定要建立的新数组长度,如果新数组的长度超过原数组的长度,则保留数组默认值。
/*
arr1 1 2 3 4 5
arr2 Arrays.copyOf(arr1,arr1.length) 1 2 3 4 5
arr3 Arrays.copyOf(arr1,10) 1 2 3 4 5 0 0 0 0 0
*/
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,10); //10
}
}
2. int length()
获取字符串的长度。
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
System.out.println(ms1.length());
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString01{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,arr.length); //arr.length
}
public int length(){
return arr.length;
}
}
输出:
4
=========================================================================================
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
System.out.println(ms1.length());
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,10); //10
}
public int length(){ //获取长度
return arr.length;
}
}
输出:
10
3.boolean isEmpty()
判断字符串是否为空。
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
System.out.println(ms1.isEmpty()); //输出
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,arr.length); //长度arr.length
}
public boolean isEmpty(){ //判断字符串是否为空
return arr.length==0;
}
}
输出:
false
===============================================================================================================
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
System.out.println(ms1.isEmpty()); //输出
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,0); // 长度0
}
public boolean isEmpty(){ //判断字符串是否为空
return arr.length==0;
}
}
输出:
true
4.char charAt(int index)
返回index处的字符
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
System.out.println(ms1.charAt(3)); //=================
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,arr.length);
}
public char charAt(int index){
if(index<0 || index >=arr.length){ //如果角标不在长度范围内,抛出异常
throw new StringIndexOutOfBoundsException();
}
return arr[index]; //返回对应的值
}
}
5.boolean equals(Object anObject)
比较字符串的内容。
0.先判空
1.自己先跟自己比
2.判断传入的数据是否是字符串
3.判断长度是否一样
4.逐一元素进行比较
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
char[] c2={'a','b','c','d'};
MyString ms2=new MyString(c2);
System.out.println(ms1.equals(ms2)); //===============
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,arr.length);
}
public boolean equals(MyString another){
if(another==null){
return false;
}
if(this==another){
return true;
}
int len1=this.arr.length;
int len2=another.arr.length;
if(len1==len2){
char[] c1=arr;
char[] c2=another.arr;
int i=0;
while(i<len1){
if(c1[i]!=c2[i]){
return false;
}
i++;
}
return true;
}else{
return false;
}
}
}
输出:
true
==============================================================================================================
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
char[] c2={'A','B','C','D'};
MyString ms2=new MyString(c2);
System.out.println(ms1.equals(ms2)); //==============
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,arr.length);
}
public boolean equals(MyString another){
if(another==null){
return false;
}
if(this==another){
return true;
}
int len1=this.arr.length;
int len2=another.arr.length;
if(len1==len2){
char[] c1=arr;
char[] c2=another.arr;
int i=0;
while(i<len1){
if(c1[i]!=c2[i]){
return false;
}
i++;
}
return true;
}else{
return false;
}
}
}
输出:
false
6.boolean equalsIgnoreCase(Object anObject)
忽略大小写比较。
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
char[] c2={'A','B','C','D'};
MyString ms2=new MyString(c2);
System.out.println(ms1.equals(ms2)); //===============
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,arr.length);
}
public boolean equalsIgnoreCase(MyString another){
if(another==null){
return false;
}
if(this==another){
return true;
}
int len1=this.arr.length;
int len2=another.arr.length;
if(len1==len2){
char[] c1=arr;
char[] c2=another.arr;
int i=0;
while(i<len1){
char ch1=c1[i];
char ch2=c2[i];
if(Character.isLetter(ch1)&&Character.isLetter(ch2)){
ch1=ch1>=97?(char)(ch1-32):ch1;
ch2=ch2>=97?(char)(ch2-32):ch2;
}
if(ch1!=ch2){
return false;
}
i++;
}
return true;
}else{
return false;
}
}
}
输出:
true
7.int compareTo(String anotherString)
按字典顺序比较两个字符串,从第一个开始的差值不相等时输出该差值。
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
char[] c2={'a','c','C','D'};
MyString ms2=new MyString(c2);
System.out.println(ms1.compareTo(ms2)); //=============
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,arr.length);
}
public int compareTo(MyString another){
if(another==null){
return -1;
}
if(this==another){
return 0;
}
char[] c1=arr;
char[] c2=another.arr;
int len1=this.arr.length;
int len2=another.arr.length;
int limi=Math.min(len1,len2);
int i=0;
while(i<limi){
if(c1[i]!=c2[i]){
return c1[i]-c2[i];
}
i++;
}
return len1-len2;
}
}
输出:
-1
=================================================================================================================
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d'};
MyString ms1=new MyString(c1);
char[] c2={'A','c','C','D'};
MyString ms2=new MyString(c2);
System.out.println(ms1.compareTo(ms2)); //=============
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,arr.length);
}
public int compareTo(MyString another){
if(another==null){
return -1;
}
if(this==another){
return 0;
}
char[] c1=arr;
char[] c2=another.arr;
int len1=this.arr.length;
int len2=another.arr.length;
int limi=Math.min(len1,len2);
int i=0;
while(i<limi){
if(c1[i]!=c2[i]){
return c1[i]-c2[i];
}
i++;
}
return len1-len2;
}
}
输出:
32
8.int compareToIgnoreCase(String str)
忽略大小写,按顺序比较。当超出长度时,比较多余的共有多少。
class Demo{
public static void main(String args[]) {
String str1 = "STRINGS";
String str2 = "Surings";
String str3 = "Strings123aaa"; //123aaa 6个
int result = str1.compareToIgnoreCase( str2 );
System.out.println(result);
result = str2.compareToIgnoreCase( str3 );
System.out.println(result);
result = str3.compareToIgnoreCase( str1 );
System.out.println(result);
}
}
输出:
-1
1
6
9.boolean startsWith(String prefix)
- prefix --前缀
从开头对比是否一样。
class MyStringDemo{
public static void main(String[] args){
char[] c1={'a','b','c','d','1'};
MyString ms1=new MyString(c1);
char[] c2={'a','b','c',};
MyString ms2=new MyString(c2);
System.out.println(ms1.startsWith(ms2)); //============
}
}
import java.util.Arrays;
import java.lang.StringIndexOutOfBoundsException;
public final class MyString{
private char[] arr;
MyString(){
arr=new char[0];
}
MyString(char[] arr){
this.arr=Arrays.copyOf(arr,arr.length);
}
public boolean startsWith(MyString prefix){
if(prefix==null){
return false;
}
if(prefix==this){
return true;
}
int len1=arr.length;
int len2=prefix.arr.length;
if(len2>len1){
return false;
}
char[] c1=arr;
char[] c2=prefix.arr;
for(int i=0;i<len2;i++){
if(c1[i]!=c2[i]){
return false;
}
}
return true;
}
}
10.boolean startsWith(String prefix, int toffset)
- prefix -- 前缀。
- toffset -- 字符串中开始查找的位置。
class Demo{
public static void main(String args[]) {
String Str = new String("www.xuexi.com");
System.out.print("返回值 :" );
System.out.println(Str.startsWith("www") );
System.out.print("返回值 :" );
System.out.println(Str.startsWith("xuexi") );
System.out.print("返回值 :" );
System.out.println(Str.startsWith("xuexi", 4) );
}
}
返回值:true
返回值:false
返回值:true
11.boolean endsWith(String suffix)
- suffix--后缀
class Test{
public static void main(String args[]) {
String str1=new String("www.xuexi.com");
String str2=new String("com");
String str3=new String("co");
System.out.print("返回值 :" );
System.out.println(str1.endsWith(str2));
System.out.print("返回值 :" );
System.out.println(str1.endsWith(str3) );
}
}
输出:
返回值:true
返回值:false
12.boolean contains(CharSequence s)
是否包含s字符串
class Test{
public static void main(String args[]) {
String str1=new String("www.xuexi.com");
String str2=new String("com");
String str3=new String("cmo");
System.out.print("返回值 :" );
System.out.println(str1.contains(str2)); //===========
System.out.print("返回值 :" );
System.out.println(str1.contains(str3) ); //===========
}
}
输出:
返回值:true
返回值:false
13.int indexOf(int ch, int fromIndex)
-
ch
- 指定的一个字符。 fromIndex
- 开始搜索的索引。
class Test{
public static void main(String args[]) {
String str1=new String("www.xuexi.com");
String str2=new String("c");
String str3=new String("cmo");
String str4=new String("com");
System.out.print("返回值 :" );
System.out.println(str1.indexOf(str2));
System.out.print("返回值 :" );
System.out.println(str1.indexOf(str2,4));
System.out.print("返回值 :" );
System.out.println(str1.indexOf(str4));
System.out.print("返回值 :" );
System.out.println(str1.indexOf(str3));
}
}
输出:
返回值:10
返回值:10
返回值:10
返回值:-1
14.int lastIndexOf(int ch)
最后出现的位置
class Test{
public static void main(String args[]) {
String str1=new String("www.xuexi.com");
String str2=new String("x");
System.out.print("返回值 :" );
System.out.println(str1.lastIndexOf(str2));
}
}
输出:
返回值:7
15.String substring(int beginIndex, int endIndex)
切取字符串
-
beginIndex
- 开头索引(包括)。 -
endIndex
- 结束索引(不包括)。
class Test{
public static void main(String args[]) {
String str1=new String("www.xuexi.com");
System.out.print("返回值 :" );
System.out.println(str1.substring(4,10));
}
}
输出:
返回值:xuexi
16.String concat(String str)
将str连接在该字符之后。
class Test{
public static void main(String args[]) {
String str1=new String("www.xuexi.com");
String str2=new String("学习");
System.out.print("返回值 :" );
System.out.println(str1.concat(str2));
}
}
输出:
返回值:www.xuexi.com学习
17.String replace(char oldChar, char newChar)
替换
- oldChar -- 原字符
- newChar -- 新字符。
class Test{
public static void main(String args[]) {
String str1=new String("www.xuexi.com");
String str2=new String("z");
System.out.print("返回值 :" );
System.out.println(str1.replace("w",str2));
}
}
输出:
返回值:zzz.xuexi.com
18.String toLowerCase()
全部装换成小写。
class Demo{
public static void main(String args[]) {
String str1=new String("WWW.xuExI.com");
System.out.print("返回值 :" );
System.out.println(str1.toLowerCase());
}
}
输出:
返回值:www.xuexi.com
19.String toUpperCase()
全部装换成大写。
class Demo{
public static void main(String args[]) {
String str1=new String("WWW.xuExI.com");
System.out.print("返回值 :" );
System.out.println(str1.toUpperCase());
}
}
输出:
返回值:WWW.XUEXI.COM
20.String trim()
去除两边的空格。
class Test{
public static void main(String args[]) {
String str1=new String(" www.xuexi.com ");
System.out.print("返回值 :" );
System.out.println(str1.trim());
}
}
输出:
返回值:www.xuexi.com