String.split 一些特殊符号的输入情况

public class StringSplit
	{
	public static void main(String[] args)
		{
		String str1 = "aa.bb.cc.dd.ee";
		String[] s1 = str1.split("\\.");
		System.out.println(s1.length);
		for (String s : s1)
			{
			System.out.println(s);
			}
		String str2 = "aa|bb|cc|dd|ee";
		String[] s2 = str2.split("\\|");
		System.out.println(s2.length);
		for (String s : s2)
			{
			System.out.println(s);
			}
		String str3 = "aa1bb|cc/dd-ee";
		String[] s3 = str3.split("1|\\||/|-"); // 可以使用|来隔开多个条件
		System.out.println(s3.length);
		for (String s : s3)
			{
			System.out.println(s);
			}
		String str4 = "aa\\bb\\cc\\dd\\ee";
		try
			{
			String[] s4 = str4.split("\\\\"); // 匹配 \ 此斜杠还真......
			System.out.println(str4);
			System.out.println("\\\\");
			System.out.println(s4.length);
			for (String s : s4)
				{
				System.out.println(s);
				}
			}
		catch (PatternSyntaxException p)
			{
			System.out.println(p.getDescription());
			System.out.println(p.getPattern());
			}
		}
	}

你可能感兴趣的:(String)