我有一堆像这样的字符串
1. INTRODUCTION
2. BASICS
3. ADVANCED CONCEPTS
4. EXAMPLES
上面的每一行都是一个单独的字符串。相同的字符串可以显示如下 -
A. INTRODUCTION
B. BASICS
C. ..
或作为
I) INTRODUCTION
II) BASICS
III) ...
或作为
10.01 INTRODUCTION
10.02 BASICS
...
所以,我试图识别(并可能消除)这些字符串之间的任何类型(数字,浮点数,罗马数字和完全未知类型)的序列。 在java中执行此操作的最佳方法是什么?
答案 0 :(得分:0)
你想分裂中间空间吗?
public class TestApp {
public static void main(String[] args) {
String[] strings = new String[] {
"1. INTRODUCTION",
"2. BASICS",
"3. ADVANCED CONCEPTS",
"4. EXAMPLES"};
for(String string : strings) {
String[] tokens = string.split(" ");
System.out.println("[" + string + "][" + tokens[0] + "][" + tokens[1] + "]");
}
}
}
输出
[1. INTRODUCTION][1.][INTRODUCTION]
[2. BASICS][2.][BASICS]
[3. ADVANCED CONCEPTS][3.][ADVANCED]
[4. EXAMPLES][4.][EXAMPLES]
如果你知道你的模式使用像这样的简单设计模式
public class TestApp {
private static IPatternStripper[] STRIPPERS = new IPatternStripper[] {
new NumeralStripper()
// more types here ...
};
public static void main(String[] args) {
String[] strings = new String[] {
"1. INTRODUCTION",
"2. BASICS",
"3. ADVANCED CONCEPTS",
"4. EXAMPLES"};
for(String string : strings) {
IPatternStripper foundStripper = null;
for(IPatternStripper stripper : STRIPPERS) {
if(stripper.isPatternApplicable(string)) {
foundStripper = stripper;
break;
}
}
if(foundStripper != null) {
System.out.println("SUCCESS: " + foundStripper.stripPattern(string));
}
else {
System.out.println("ERROR: NO STRIPPER CAN PROCESS: " + string);
}
}
}
}
interface IPatternStripper {
public boolean isPatternApplicable(String line);
public String stripPattern(String line);
}
class NumeralStripper implements IPatternStripper {
@Override
public boolean isPatternApplicable(String line) {
boolean result = false;
// code here checks whether this stripper is appropriate
return result;
}
@Override
public String stripPattern(String line) {
String value = line;
// code here to do your stripping
return value;
}
}