OSDN Git Service

d7af4cbd3a5cb85fc0645cf5cb2086582f46c421
[traindelaybot/source.git] / workspace / .metadata / .plugins / org.eclipse.core.resources / .history / 0 / 80d0300c106b001115b7b82f2f4ce01a
1 package com.yuji.tdb.utility;
2
3 import java.util.regex.Pattern;
4
5 public class StringUtility {
6         private static Pattern ptnAlnum = Pattern.compile("[a-zA-Z0-9]");
7         private static Pattern ptnSpace = Pattern.compile("\\s");
8         private static Pattern ptnAscii = Pattern.compile("\\p{ASCII}");
9         
10         public static String parseSubstring(String text, int length){
11                 int len = text.length();
12                 int status = -1;
13                 int pos = 0;
14                 
15                 for (int i = 0; i < len + 1 && i < length + 1; i++){
16                         if (i >= len){
17                                 pos = i;
18                                 break;
19                         }
20                         String ch = text.substring(i, i + 1);
21                         
22                         if (ptnAlnum.matcher(ch).matches()){
23                                 if (status != 0){
24                                         if (status != 1){
25                                                 pos = i;
26                                         }
27                                         status = 0;
28                                 }
29                         }
30                         else if (ptnSpace.matcher(ch).matches()){
31                                 if (status != 1){
32                                         pos = i;
33                                         status = 1;
34                                 }
35                         }
36                         else if (ptnAscii.matcher(ch).matches()){
37                                 if (status != 2){
38                                         if (status != 1){
39                                                 pos = i;
40                                         }
41                                         status = 2;
42                                 }                               
43                         }
44                         else {
45                                 pos = i;
46                                 status = 3;
47                         }
48                 }
49                 if (pos == 0){
50                         return text.substring(0, (len < length)? len : length);                 
51                 }
52                 return text.substring(0, pos);
53         }
54 }