1.String类中的 matches(String regex)方法:
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
还是个告诉这个字符串是否匹配给定的regular expression 。
这种形式为str .matches(
regex )
方法的)
产生与表达式完全相同的结果
示例:
package com.ftl818;
public class Test {
public static void main(String[] args) {
String str="hsghdghsgh";
String str1="hfhdf^ddsf$nn";
System.out.println(str.matches("sgh"));
System.out.println(str.matches("hsghdghsgh"));
System.out.println(str.matches("[a-z]{10}"));
}
}
输出:
2.String类中的 contains(CharSequence s)方法:
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}
当且仅当此字符串包含指定的char值序列时才返回true。
示例:
package com.ftl818;
public class Test {
public static void main(String[] args) {
String str="hsghdghsgh";
System.out.println(str.contains("sgh"));
System.out.println(str.contains("llkl"));
}
}
输出:
3.String类中的 split(String regex, int limit)方法:
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
regex.charAt(0) == '\\' &&
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
((ch-'a')|('z'-ch)) < 0 &&
((ch-'A')|('Z'-ch)) < 0)) &&
(ch < Character.MIN_HIGH_SURROGATE ||
ch > Character.MAX_LOW_SURROGATE))
{
int off = 0;
int next = 0;
boolean limited = limit > 0;
ArrayList<String> list = new ArrayList<>();
while ((next = indexOf(ch, off)) != -1) {
if (!limited || list.size() < limit - 1) {
list.add(substring(off, next));
off = next + 1;
} else { // last one
//assert (list.size() == limit - 1);
list.add(substring(off, value.length));
off = value.length;
break;
}
}
// If no match was found, return this
if (off == 0)
return new String[]{this};
// Add remaining segment
if (!limited || list.size() < limit)
list.add(substring(off, value.length));
// Construct result
int resultSize = list.size();
if (limit == 0) {
while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
resultSize--;
}
}
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);
}
return Pattern.compile(regex).split(this, limit);
}
围绕给定的模式匹配分割这个字符串。返回字符串数组。
这个方法返回的数组包含这个字符串的每一个子字符串,这些子字符串被另一个匹配给定表达式的子字符串终止,或者在字符串结束时终止。数组中的子字符串按照它们在这个字符串中出现的顺序排列。如果表达式不匹配输入的任何部分,那么结果数组只有一个元素,即这个字符串。
当在此字符串的开始处存在正宽度匹配时,在结果数组的开始处包含空的前导子字符串。 开始时的零宽度匹配不会产生这样的空的前导子串。
limit
参数控制应用模式的次数,因此影响生成的数组的长度。 如果极限n大于0,则模式最多应用n -1次,数组的长度不大于n ,数组的最后一个条目将包含超出最后一个匹配分隔符的所有输入。 如果n是非正的,那么模式将被应用到尽可能多的次数,并且数组可以有任何长度。 如果n为0,则模式将被应用尽可能多次,数组可以有任何长度,并且尾随的空字符串将被丢弃。
示例:
package com.ftl819;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
String str="ab-c--bcde-d--";
String[] st= str.split("-", 3);
for (String string : st) {
System.out.println(string);
}
System.out.println("===========================");
String[] st1= str.split("-", 10);
for (String string : st1) {
System.out.println(string);
}
System.out.println("++++++++++++++++++++++=+++++++++");
String[] st2= str.split("-", 0);
for (String string : st2) {
System.out.println(string);
}
System.out.println("*********************************");
String[] st3= str.split("-", -1);
for (String string : st3) {
System.out.println(string);
}
System.out.println("#################################");
}
}
输出:
4.String类中的split(String regex)方法:
public String[] split(String regex) {
return split(regex, 0);
}
将此字符串拆分为给定的regular expression的匹配。
该方法的工作原理是通过使用给定表达式和限制参数为零调用双参数split
方法。 因此,尾随的空字符串不会包含在结果数组中。
该方法是split(String regex, int limit)方法的特殊版,不输入limit的值,默认limit的值为0.