http://www.verejava.com/?id=16994950342031
package com.reg;
public class TestSplit
{
public static void main(String[] args)
{
testContry();
}
/**
* 4. 将下面的国家分割打印
ChinaqqqAmericahhhhhEnglandaaaaaaMexica
*/
public static void testContry()
{
String country="ChinaqqqqqAmericahhhEnglandaaaaaaMexica";
String[] countrys=country.split("(.)\\1+");
for(String str:countrys)
{
System.out.println(str);
}
}
/**
* 3. 将下面小学生的成绩分割打印出来
数学=100,语文=69;英语=70,计算机=90:地理=85,体育=50
*/
public static void testScore()
{
String score="数学=100,语文=69;英语=70,计算机=90:地理=85,体育=50";
String[] scores=score.split("[,;:]");
for(String str:scores)
{
System.out.println(str);
}
}
/**
* String str=”薪水 职位 姓名 年龄 性别”;
*/
public static void testSpace()
{
/*String info="薪水 职位 姓名 年龄 性别";
String[] infoArray=info.split(" +");*/
String info="薪水 职位 姓名 年龄 性别";
String[] infoArray=info.split("[ \t]+");
for(String str:infoArray)
{
System.out.println(str);
}
}
/**
* 1. 以点号 . 切割IP 地址判断是否为C类IP地址
C类IP地址:192.168.0.0~192.168.255.255
*/
public static void testCIP(String ip)
{
String[] ips=ip.split("\\.");
for(String str:ips)
{
System.out.println(str);
}
}
}

本文介绍了一种使用Java进行字符串分割的方法,并通过实例演示了如何针对不同格式的数据进行有效分割,包括国家名称、学生成绩及IP地址等。
1185

被折叠的 条评论
为什么被折叠?



