String类以及String的常用方法

抄书。不熬夜了。

 概述:java.lang.String 类代表字符串。java中所有的字符串文字“aaa”等,都看作是该类的实例。

特点:1.字符串不变,字符串被创建后不可被更改。

    String s = "abc";

    String s+="d";   //s="abcd"

    内存中存在了两个字符串对象 “abc” 和" abcd"  ."abcd" 的引用指向了s;

   2.因为String对象不可变,存在字符串常量池中,可以被共享。

    String s1="abc";

    String s2="abc";

    内存中只存在“abc”这个字符串对象;s1和s2都指向“abc”这个对象。

   3."abc" 等效于 char[ ] date = {'a','b','c'}, 值相等,引用不一致

     String str ="abc"

    相当于:

    char[] date ={'a','b','c'};

    String str = new String(date);   

    public class StringTest {
     public static void main(String[] args) {
   String str = "abc";
   char[] date = {'a','b','c'};
   String str1 = new String(date);
   System.out.println(str.equals(str1));//true
     }
    }

String类的构造方法:

  • public String(): 初始化新创建的String对象,以使其表示空字符串序列。
  • public String(char[] value):通过当前参数中的字符数组来构造新的String;
  • public String(byte[] bytes):通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String; 

   //无参构造方法   String str = new String();

  //字符数组  char[] date = {'a','b','c'};

  String str = new String(date);

  //字节数组  byte[] bytes = {97,98,99};

  String str = new String(byte);

String类的常用方法:

  public boolean equals (Object anObject) :将此字符串与指定对象进行比较。 

  public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小 写。 

  public int length () :返回此字符串的长度。 

  public String concat (String str) :将指定的字符串连接到该字符串的末尾。 

  public char charAt (int index) :返回指定索引处的 char值。 

  public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。 

  public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符 串结尾。 

  public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。

  public char[] toCharArray () :将此字符串转换为新的字符数组。 

  public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。 

  public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使 用replacement字符串替换。

  public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。

常用方法的演示:

/**
* public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
 public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。
*/
public class StringTest01 {
public static void main(String[] args) {
//创建字符串对象
String str ="hello";
String str1 ="hello";
String str2 ="Hello";
//区分大小写比较
//public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
System.out.println(str.equals(str1));//true
System.out.println(str.equals(str2));//false

//不区分大小写比较
//public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。
System.out.println(str.equalsIgnoreCase(str2));//true
System.out.println(str1.equalsIgnoreCase(str2));//true

}
}

/**
* public int length () :返回此字符串的长度。
  public String concat (String str) :将指定的字符串连接到该字符串的末尾。
  public char charAt (int index) :返回指定索引处的 char值。
  public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
  public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符 串结尾。
  public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。
*/
public class StringTest02 {
public static void main(String[] args) {
//创建字符串
String str = "helloworld";
//public int length () :返回此字符串的长度。
System.out.println(str.length());//10
//public String concat (String str) :将指定的字符串连接到该字符串的末尾。
String str1 = str.concat(",nihao");
System.out.println(str1);//helloworld,nihao
//public char charAt (int index) :返回指定索引处的 char值。
char c = str.charAt(0);
System.out.println(c);//h
//public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
int l = str.indexOf("l");
System.out.println(l);//2
//public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
String s = str.substring(5);
System.out.println(s);//world
//ublic String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到 endIndex截取字符串。
// 含beginIndex,不含endIndex。
String s1 = str.substring(4, 7);
System.out.println(s1);//owo
}
}

/**
* public char[] toCharArray () :将此字符串转换为新的字符数组。
* public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
* public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使 用replacement字符串替换。
*/
public class StringTest03 {
public static void main(String[] args) {
String str = "abcdef";

//public char[] toCharArray () :将此字符串转换为新的字符数组。
char[] chars = str.toCharArray();
for (int i = 0; i <chars.length ; i++) {
System.out.print(chars[i]+" ");//a b c d e f
}
//public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
byte[] bytes = str.getBytes();
for (int i = 0; i <bytes.length ; i++) {
System.out.print(bytes[i]+" ");//97 98 99 100 101 102
}
//public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。
String replace = str.replace("bcdef", "AAAAA");
System.out.println(replace);//aAAAAA
}
}
/**
* public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
*/
public class StringTest04 {
public static void main(String[] args) {
String str = "helle_world_xxx";
String[] s1 = str.split("_");//["helle","world","xxx"]
for (int i = 0; i <s1.length ; i++) {
System.out.print(s1[i]+" ");//helle world xxx
}
}
}

String类方法的练习:

  1.定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。格式参照如下:[word1#word2#word3]。  

/**
* 定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。格式参照如下:[word1#word2#word3]。
*/
public class StringTest05 {
public static void main(String[] args) {
//定义一个数组
int[] a = {1,2,3};
System.out.print("[");
for (int i = 0; i <a.length ; i++) {
System.out.print(a[i]);
if(i != a.length-1){
System.out.print("#");
}else {
System.out.print("]");
}
}
}
}

  2.统计字符个数:键盘录入一个字符,统计字符串中大小写字母及数字字符个数

  转为字符的方式:

  

/**
*统计字符个数:键盘录入一个字符,统计字符串中大小写字母及数字字符个数
*/
public class StringTest07 {
public static void main(String[] args){

Scanner in = new Scanner(System.in);
System.out.println("请输入字符串:");
String s = in.next();
int numCount=0;
int bigCount=0;
int smallCount=0;

for (int i = 0; i <s.length() ; i++) {
char c = s.charAt(i);
if (c>='0'&&c<='9'){
numCount++;
}else if(c>='a'&&c<='z'){
smallCount++;
}else if(c>='A'&&c<='Z'){
bigCount++;
}
}

System.out.println("数字共有:"+numCount+" 个");
System.out.println("大写字母共有:"+bigCount+" 个");
System.out.println("小写字母共有:"+smallCount+" 个");
}
}

 转为字节数组的方式:

/**
*统计字符个数:键盘录入一个字符,统计字符串中大小写字母及数字字符个数
*/
public class StringTest06 {
public static void main(String[] args){

Scanner in = new Scanner(System.in);
System.out.println("请输入字符串:");
String s = in.next();
int number=0;
int bigWord=0;
int small=0;
byte[] bytes = s.getBytes();
for (int i = 0; i <bytes.length ; i++) {
if(bytes[i]>=48&&bytes[i]<=57){
number+=1;
}else if(bytes[i]>=65&&bytes[i]<=90){
bigWord+=1;
}else if(bytes[i]>=97&&bytes[i]<=127){
small+=1;
}
}
System.out.println("数字共有:"+number+" 个");
System.out.println("大写字母共有:"+bigWord+" 个");
System.out.println("小写字母共有:"+small+" 个");
}
}

 

原文地址:https://www.cnblogs.com/lifengSkt/p/13258633.html