[Java]split(".")失效的解决办法

String str4 = "115.157.200.135";
		String splitedArray [] = str4.split("\\.");
		System.out.println("SplitedArray.length = "+splitedArray.length);
		for (int i = 0 ; i < splitedArray.length ; i++) {
			System.out.print(splitedArray[i]+"\t");
		}

输出结果:

原因分析:

str.split()的参数是一个正则表达式,正则表达式中的元字符必须使用转义符号进行转义

这里,在正则表达式中"."表示的并不是'.'自身,而是:

. 匹配除 “\n” 之外的任何单个字符。要匹配包括 ‘\n’ 在内的任何字符,请使用象 ‘[.\n]’ 的模式。

猜你喜欢

转载自blog.csdn.net/chenhanxuan1999/article/details/91399823