OSDN Git Service

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