Java教程 第16页

Java split() 方法-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java split() 方法

Java split() 方法 Java String类 split() 方法根据匹配给定的正则表达式来拆分字符串。 语法 public String[] split(String regex, int limit) 参数 regex — 正则表达式分隔符。 limit — 分割的份数。 返回值 成功则返回替换的字符串,失败则返回原始字符串。 实例 public class Test { public static void main(String args[]) { String Str = new String("Welcome-to-W3CSchool.com"); System.out.println("返回值 :" ); for (String retval: Str.split("-", 2)){ System.out.println(retval); } System.out.println(""); System.out.println("返回值 :" ); for (String retval: Str.split("-", 3)){ System.out.println(retval); } System.out.println(""); System.out.println("返回值 :" ); for (String retval: Str.split("-", 0)){ System.out.println(retval); } System.out.println(""); System.out.println("返回值 :" ); for (String retval: Str.split("-")){ System.out.println(retval); } } } 以上程序执行结果为: 返回值 : Welcome to-W3CSchool.com 返回值 : Welcome to W3CSchool.com 返回值 : Welcome to W3CSchool.com 返回值 : Welcome to W3CSchool.com Java String类

Java replaceFirst() 方法-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java replaceFirst() 方法

Java replaceFirst() 方法 Java String类 replaceFirst() 方法使用给定的参数 replacement 替换字符串第一个匹配给定的正则表达式的子字符串。 语法 public String replaceFirst(String regex, String replacement) 参数 regex — 匹配此字符串的正则表达式。 replacement — 用来替换第一个匹配项的字符串。 返回值 成功则返回替换的字符串,失败则返回原始字符串。 实例 public class Test { public static void main(String args[]) { String Str = new String("hello youj,I am from youj。"); System.out.print("返回值 :" ); System.out.println(Str.replaceFirst("youj", "google" )); System.out.print("返回值 :" ); System.out.println(Str.replaceFirst("(.*)youj(.*)", "google" )); } } 以上程序执行结果为: 匹配成功返回值 :youj 匹配失败返回值 :www.google.com Java String类

Java replaceAll() 方法-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java replaceAll() 方法

Java replaceAll() 方法 Java String类 replaceAll() 方法使用给定的参数 replacement 替换字符串所有匹配给定的正则表达式的子字符串。 语法 public String replaceAll(String regex, String replacement) 参数 regex — 匹配此字符串的正则表达式。 newChar — 用来替换每个匹配项的字符串。 返回值 成功则返回替换的字符串,失败则返回原始字符串。 实例 public class Test { public static void main(String args[]) { String Str = new String("www.google.com"); System.out.print("匹配成功返回值 :" ); System.out.println(Str.replaceAll("(.*)google(.*)", "youj" )); System.out.print("匹配失败返回值 :" ); System.out.println(Str.replaceAll("(.*)taobao(.*)", "youj" )); } } 以上程序执行结果为: 匹配成功返回值 :youj 匹配失败返回值 :www.google.com Java String类

Java replace() 方法-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java replace() 方法

Java replace() 方法 Java String类 replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。 语法 public String replace(char oldChar, char newChar) 参数 oldChar — 原字符。 newChar — 新字符。 返回值 替换后生成的新字符串。 实例 public class Test { public static void main(String args[]) { String Str = new String("hello"); System.out.print("返回值 :" ); System.out.println(Str.replace('o', 'T')); System.out.print("返回值 :" ); System.out.println(Str.replace('l', 'D')); } } 以上程序执行结果为: 返回值 :hellT 返回值 :heDDo Java String类

Java length() 方法-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java length() 方法

Java length() 方法 Java String类 length() 方法用于返回字符串的长度。 长度等于字符串中 16 位 Unicode 代码单元的数量。 语法 public int length() 参数 无 返回值 返回字符串长度。 实例 public class Test { public static void main(String args[]) { String Str1 = new String("www.w3cschool.cn"); String Str2 = new String("youj" ); System.out.print("字符串 Str1 长度 :"); System.out.println(Str1.length()); System.out.print("字符串 Str2 长度 :"); System.out.println(Str2.length()); } } 以上程序执行结果为: 字符串 Str1 长度 :14 字符串 Str2 长度 :6 Java String类

Java intern() 方法-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java intern() 方法

Java intern() 方法 Java String类 intern() 方法返回字符串对象的规范化表示形式。 它遵循以下规则:对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。 语法 public String intern() 参数 无 返回值 一个字符串,内容与此字符串相同,但一定取自具有唯一字符串的池。 实例 public class Test { public static void main(String args[]) { String Str1 = new String("www.w3cschool.cn"); String Str2 = new String("www.w3cschool.cn"); System.out.print("规范表示:" ); System.out.println(Str1.intern()); System.out.print("规范表示:" ); System.out.println(Str2.intern()); } } 以上程序执行结果为: 规范表示:www.w3cschool.cn 规范表示:www.w3cschool.cn Java String类

Java 字符串搜索-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 字符串搜索

Java数据类型教程 – Java字符串搜索 我们可以使用indexOf()和lastIndexOf()方法获取另一个字符串中的字符或字符串的索引。例如 public class Main { public static void main(String[] args) { String str = new String("Apple"); int index = str.indexOf("p"); // index will have a value of 1 System.out.println(index); index = str.indexOf("pl"); // index will have a value of 2 System.out.println(index); index = str.lastIndexOf("p"); // index will have a value of 2 System.out.println(index); index = str.lastIndexOf("pl"); // index will have a value of 2 System.out.println(index); index = str.indexOf("k"); // index will have a value of -1 System.out.println(index); } } 上面的代码生成以下结果。 indexOf()方法从字符串的开头开始搜索字符或字符串,并返回第一个匹配的索引。 lastIndexOf()方法从末尾匹配字符或字符串,并返回第一个匹配的索引。 如果在字符串中没有找到字符或字符串,这些方法返回-1。 匹配字符串的开始和结束 startsWith()检查字符串是否以指定的参数开头,而endsWith()检查字符串是否以指定的字符串参数结尾。 两个方法都返回一个布尔值。 public class Main { public static void main(String[] args) { String str = "This is a test"; // Test str, if it starts with "This" if (str.startsWith("This")) { System.out.println("String starts with...

Java hashCode() 方法-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java hashCode() 方法

Java hashCode() 方法 Java String类 hashCode() 方法用于返回字符串的哈希码。 字符串对象的哈希码根据以下公式计算: s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 使用 int 算法,这里 s[i] 是字符串的第 i 个字符,n 是字符串的长度,^ 表示求幂。空字符串的哈希值为 0。 语法 public int hashCode() 参数 无。 返回值 返回对象的哈希码值。 实例 public class Test { public static void main(String args[]) { String Str = new String("www.w3cschool.cn"); System.out.println("字符串的哈希码为 :" + Str.hashCode() ); } } 以上程序执行结果为: 字符串的哈希码为 :321005537 Java String类

Java getChars() 方法-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java getChars() 方法

Java getChars() 方法 Java String类 getChars() 方法将字符从字符串复制到目标字符数组。 语法 public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 参数 srcBegin — 字符串中要复制的第一个字符的索引。 srcEnd — 字符串中要复制的最后一个字符之后的索引。 dst — 目标数组。 dstBegin — 目标数组中的起始偏移量。 返回值 没有返回值,但会抛出 IndexOutOfBoundsException 异常。 实例 public class Test { public static void main(String args[]) { String Str1 = new String("www.w3cschool.cn"); char[] Str2 = new char[6]; try { Str1.getChars(4, 10, Str2, 0); System.out.print("拷贝的字符串为:" ); System.out.println(Str2 ); } catch( Exception ex) { System.out.println("触发异常..."); } } } 以上程序执行结果为: 拷贝的字符串为:youj Java String类

Java getBytes() 方法-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java getBytes() 方法

Java getBytes() 方法 Java String类 getBytes() 方法有两种形式: getBytes(String charsetName):使用平台的默认字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 getBytes(): 使用平台的默认字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 语法 public byte[] getBytes(String charsetName) throws UnsupportedEncodingException 或 public byte[] getBytes() 参数 charsetName — 支持的字符集名称。 返回值 返回 byte 数组。 实例 import java.io.*; public class Test { public static void main(String args[]) { String Str1 = new String("youj"); try{ byte[] Str2 = Str1.getBytes(); System.out.println("返回值:" + Str2 ); Str2 = Str1.getBytes( "UTF-8" ); System.out.println("返回值:" + Str2 ); Str2 = Str1.getBytes( "ISO-8859-1" ); System.out.println("返回值:" + Str2 ); } catch ( UnsupportedEncodingException e){ System.out.println("不支持的字符集"); } } } 以上程序执行结果为: 返回值:[B@7852e922 返回值:[B@4e25154f 返回值:[B@70dea4e Java String类