Java入门基础之【String类】11

来源:互联网 发布:北京大学 网络课程 编辑:程序博客网 时间:2024/05/20 00:11

Java String 类

字符串广泛应用 在Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。

注:String类是不可改变的,经常修改可以使用StringBuffer或StringBuider类。

创建字符串

创建字符串最简单的方式如下:

String str = "我是String类";

在代码中遇到字符串常量时,这里的值是 "我是String类"",编译器会使用该值创建一个 String 对象。

和其它对象一样,可以使用关键字和构造方法来创建 String 对象。

String 类有 11 种构造方法,这些方法提供不同的参数来初始化字符串,比如提供一个字符数组参数:

public class TestDemo {   
public static void main(String[] args) {
char[] hello = { 'a', 'b', 'c', 'd', 'e'};
String strHel = new String(hello);
System.out.println(strHel);
}
}

以上实例编译运行结果如下:

abcde

字符串长度

String类提供了一个length()方法,返回字符串的字符数。

实例

public class TestDemo {   
public static void main(String[] args) {
String strHel = "baidui.com";
int a = strHel.length();
System.out.println("百度网址长度为:" + a);
}
}

以上实例编译运行结果如下:

百度网址长度为:10

连接字符串

String 类提供了连接两个字符串的方法:

string1.concat(string2);
string1+string2;

实例

public class TestDemo {   
public static void main(String[] args) {
String str = "百度网址是:";
String strHel = "baidui.com";
String sums = str + strHel;
System.out.println("我是连接字符串+:" + sums);
}
}

以上实例编译运行结果如下:

我是连接字符串+:百度网址是:baidui.com

创建格式化字符串

我们知道输出格式化数字可以使用 printf()format() 方法。

String 类使用静态方法 format() 返回一个String 对象而不是 PrintStream 对象。

String 类的静态方法 format() 能用来创建可复用的格式化字符串,而不仅仅是用于一次打印输出。

如下所示:

public class TestDemo {   
public static void main(String[] args) {
String strHel = "baidui.com";
float  f = 3f;
System.out.printf("我是连接字符串+:%s"+",浮点型值是:%f",strHel,f );
}
}

你也可以这样写

public class TestDemo {   
public static void main(String[] args) {
String strHel = "baidui.com";
float  f = 3f;
String fs ;
fs = String.format("我是连接字符串+:%s"+",浮点型值是:%f", strHel,f);
System.out.println(fs);
}
}

以上实例编译运行结果如下:

我是连接字符串+:baidui.com,浮点型值是:3.000000

String 方法

下面是 String 类支持的方法,更多详细,参看 Java String API 文档:

SN(序号)方法描述1

char charAt(int index)

返回指定索引处的 char 值。2

int compareTo(Object o)

把这个字符串和另一个对象比较。3

int compareTo(String anotherString)

按字典顺序比较两个字符串。4

int compareToIgnoreCase(String str)

按字典顺序比较两个字符串,不考虑大小写。5

String concat(String str)

将指定字符串连接到此字符串的结尾。6

boolean contentEquals(StringBuffer sb)

当且仅当字符串与指定的StringButter有相同顺序的字符时候返回真。7

static String copyValueOf(char[] data)

返回指定数组中表示该字符序列的 String。(数组类型转换String类型)8

static String copyValueOf(char[] data,int offset,int count)

返回指定数组中表示该字符序列的 String。(数组类型转换String类型)9

boolean endsWith(String suffix)

测试此字符串是否以指定的后缀结束。10

boolean equals(Object obj)

将此字符串与指定的对象比较。11

boolean equalsIgnoreCase(String anotherString)

将此 String 与另一个 String 比较,不考虑大小写。12byte[] getBytes()
 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。13byte[] getBytes(String charsetName)
使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。14void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。15int hashCode()
返回此字符串的哈希码。16int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。17int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。18int indexOf(String str)
 返回指定子字符串在此字符串中第一次出现处的索引。19int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。20String intern()
 返回字符串对象的规范化表示形式。21int lastIndexOf(int ch)
 返回指定字符在此字符串中最后一次出现处的索引。22int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。23int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引。24int lastIndexOf(String str, int fromIndex)
 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。25int length()
返回此字符串的长度。26boolean matches(String regex)
告知此字符串是否匹配给定的正则表达式。27boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。28boolean regionMatches(int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。29String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。30String replaceAll(String regex, String replacement
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。31String replaceFirst(String regex, String replacement)
 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。32String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。33String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串。34boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。35boolean startsWith(String prefix, int toffset)
测试此字符串从指定索引开始的子字符串是否以指定前缀开始。36CharSequence subSequence(int beginIndex, int endIndex)
 返回一个新的字符序列,它是此序列的一个子序列。37String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。38String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。39char[] toCharArray()
将此字符串转换为一个新的字符数组。40String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。41String toLowerCase(Locale locale)
 使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。42String toString()
 返回此对象本身(它已经是一个字符串!)。43String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。44String toUpperCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。45String trim()
返回字符串的副本,忽略前导空白和尾部空白。46static String valueOf(primitive data type x)
返回给定data type类型x参数的字符串表示形式。

Java charAt() 方法


charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。

语法

public char charAt(int index)

参数

  • index -- 字符的索引。

返回值

返回指定索引处的字符。

实例

public class TestDemo {   public static void main(String[] args) {String str = "abc.com";char result = str.charAt(2);System.out.println(result);}}

以上程序执行结果为:

c

Java compareTo() 方法


compareTo() 方法用于两种方式的比较:

  • 字符串与对象进行比较。
  • 按字典顺序比较两个字符串。

语法

int compareTo(Object o)int compareTo(String anotherString)

参数

  • o -- 要比较的对象。

  • anotherString -- 要比较的字符串。

返回值

  • 如果参数字符串等于此字符串,则返回值 0;
  • 如果此字符串小于字符串参数,则返回一个小于 0 的值;
  • 如果此字符串大于字符串参数,则返回一个大于 0 的值。

实例

public class TestDemo {   public static void main(String[] args) {String str1 = "abc";String str2 = "abc";String str3 = "abcdef";int res1 = str1.compareTo(str2);System.out.println("字符串相等:"+res1);int res2 = str1.compareTo(str3);System.out.println("小于要比较的字符串:"+res2);int res3 = str3.compareTo(str1);System.out.println("大于要比较的字符串:"+res3);}}

以上程序执行结果为:

字符串相等:0小于要比较的字符串:-3大于要比较的字符串:3

Java compareToIgnoreCase() 方法


compareToIgnoreCase() 方法用于按字典顺序比较两个字符串,不考虑大小写。

语法

int compareToIgnoreCase(String str)

参数

  • str -- 要比较的字符串。

返回值

  • 如果参数字符串等于此字符串,则返回值 0;
  • 如果此字符串小于字符串参数,则返回一个小于 0 的值;
  • 如果此字符串大于字符串参数,则返回一个大于 0 的值。

实例

public class TestDemo {   public static void main(String[] args) {String str1 = "abc";String str2 = "abc";String str3 = "abcdef";int res1 = str1.compareToIgnoreCase(str2);System.out.println("字符串相等:"+res1);int res2 = str1.compareToIgnoreCase(str3);System.out.println("小于要比较的字符串:"+res2);int res3 = str3.compareToIgnoreCase(str1);System.out.println("大于要比较的字符串:"+res3);}}

以上程序执行结果为:

字符串相等:0小于要比较的字符串:-3大于要比较的字符串:3

Java concat() 方法


concat() 方法用于将指定的字符串参数连接到字符串上。

语法

public String concat(String s)

参数

  • s -- 要连接的字符串。

返回值

返回连接后的新字符串。

实例

public class TestDemo {   public static void main(String[] args) {String str = "abc";str=str.concat("def");System.out.println("字符串参数连接到字符串上:"+str);}}

以上程序执行结果为:

字符串参数连接到字符串上:abcdef

Java contentEquals() 方法


contentEquals() 方法用于将将此字符串与指定的 StringBuffer 比较。

语法

public boolean contentEquals(StringBuffer sb)

参数

  • sb -- 要与字符串比较的 StringBuffer。

返回值

如字符串与指定 StringBuffer 表示相同的字符序列,则返回 true;否则返回 false。

实例

public class TestDemo {   public static void main(String[] args) {String str = "abc";String str1 = "def";StringBuffer sb = new StringBuffer("abc");boolean res = str.contentEquals(sb);System.out.println("字符串相等:"+res); res = str1.contentEquals(sb); System.out.println("字符串不相等:"+res);}}

以上程序执行结果为:

字符串相等:true字符串不相等:false

Java copyValueOf() 方法


copyValueOf() 方法有两种形式:

  • public static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的字符串。

  • public static String copyValueOf(char[] data, int offset, int count): 返回指定数组中表示该字符序列的 字符串。

语法

public static String copyValueOf(char[] data)public static String copyValueOf(char[] data, int offset, int count)

参数

  • data -- 字符数组。

  • offset -- 子数组的初始偏移量。。

  • count -- 子数组的长度。

返回值

如字符串与指定 StringBuffer 表示相同的字符序列,则返回 true;否则返回 false。

实例

public class TestDemo {   public static void main(String[] args) {char[] ch = {'h','e','l','l','o'};String str = "";str = str.copyValueOf(ch);System.out.println("返回数组中的数:"+str);str = str.copyValueOf(ch, 2, 3);System.out.println("返回数组中指定的数:"+str);}}

以上程序执行结果为:

返回数组中的数:hello返回数组中指定的数:llo

Java endsWith() 方法


endsWith() 方法用于测试字符串是否以指定的后缀结束。

语法

public boolean endsWith(String suffix)

参数

  • suffix -- 指定的后缀。

返回值

如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true。

实例

public class TestDemo {   public static void main(String[] args) {String str ="程序员:chengxy.com";boolean res = str.endsWith("abc");System.out.println("后缀是否正确:"+res);res = str.endsWith("com");System.out.println("后缀是否正确:"+res);}}

以上程序执行结果为:

后缀是否正确:false后缀是否正确:true

Java equals() 方法


equals() 方法用于将字符串与指定的对象比较。

语法

public boolean equals(Object anObject)

参数

  • anObject -- 与字符串进行比较的对象。

返回值

如果给定对象与字符串相等,则返回 true;否则返回 false。

实例

public class TestDemo {   public static void main(String[] args) {String str ="abc";String str1 ="abc";String str2 ="edf";boolean res = str.equals(str1);System.out.println("字符串是否相等:"+res);res = str1.equals(str2);System.out.println("字符串是否相等:"+res);}}

以上程序执行结果为:

字符串是否相等:true字符串是否相等:false

Java equalsIgnoreCase() 方法


equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写。

语法

public boolean equalsIgnoreCase(String anotherString)

参数

  • anObject -- 与字符串进行比较的对象。

返回值

如果给定对象与字符串相等,则返回 true;否则返回 false。

实例

public class TestDemo {   public static void main(String[] args) {String str ="abc";String str1 ="abc";String str2 ="ABC";boolean res = str.equalsIgnoreCase(str1);System.out.println("字符串是否相等:"+res);res = str1.equalsIgnoreCase(str2);System.out.println("字符串大小写:"+res);}}

以上程序执行结果为:

字符串是否相等:true字符串大小写:true

Java getBytes() 方法


getBytes() 方法有两种形式:

  • getBytes(String charsetName): 使用指定的字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

  • getBytes(): 使用平台的默认字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

语法

public byte[] getBytes(String charsetName) throws UnsupportedEncodingExceptionpublic byte[] getBytes()

参数

  • charsetName -- 支持的字符集名称。

返回值

返回 byte 数组。

实例

public class TestDemo {   public static void main(String[] args) {String str ="abc";try {byte[] by = str.getBytes();System.out.println("默认编码:"+by);by = str.getBytes("UTF-8");System.out.println("UTF8编码:"+by);by = str.getBytes("GBK");System.out.println("GBK编码:"+by);} catch (UnsupportedEncodingException e) {e.printStackTrace();}}}

以上程序执行结果为:

默认编码:[B@15db9742UTF8编码:[B@6d06d69cGBK编码:[B@7852e922

Java getChars() 方法


getChars() 方法将字符从字符串复制到目标字符数组

语法

public void getChars(int srcBegin, int srcEnd, char[] dst,  int dstBegin)

参数

  • srcBegin -- 字符串中要复制的第一个字符的索引。

  • srcEnd -- 字符串中要复制的最后一个字符之后的索引。

  • dst -- 目标数组。

  • dstBegin -- 目标数组中的起始偏移量。

返回值

没有返回值,但会抛出 IndexOutOfBoundsException 异常。

实例

public class TestDemo {   public static void main(String[] args) {String str ="hello";char[] ch = new char[6];str.getChars(1, 3, ch, 0);System.out.print("复制的字符串是:");System.out.println(ch);}}

以上程序执行结果为:

复制的字符串是:el





原创粉丝点击