为什么我的string.length减0,如果我拆分此字符串? [英] why is my string.length 0 if I split this string?
本文介绍了为什么我的string.length减0,如果我拆分此字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个非常简单的问题,但我会先给你code。
This is probably a very easy question but I'm going to give you the code first.
import java.util.Scanner;
public class help {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Give: ");
String s = sc.next();
String[] parts = s.split(".");
System.out.println(parts.length);
}
}
就算我给192.168.1.1.1.1.1或1.2.3或...的parts.length将始终为0,可以有人请向我解释为什么,我该如何让它4,如果我进入1.2 .3.4?
Even if I give 192.168.1.1.1.1.1 or 1.2.3 or ... the parts.length will always be 0, can somebody please explain to me why and how I can let it be 4 if i enter 1.2.3.4?
推荐答案
您需要 s.split(\\\\)
因为参数拆分
是一个普通的前pression。在。
字符在常规的前pression意味着任何字符,你需要用反斜杠来逃避它有它的意思是点。
You need s.split("\\.")
because the argument to split
is a regular expression. The .
character in a regular expression means "any character", and you need to escape it with the backslash to have it mean "dot".
这篇关于为什么我的string.length减0,如果我拆分此字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文