OSDN Git Service

StringUtility
[traindelaybot/source.git] / workspace / .metadata / .plugins / org.eclipse.core.resources / .history / 49 / 700d25d1ca6a0011194396142d5d2649
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 && i < length; i++){
15                         String ch = text.substring(i, i + 1);
16                         
17                         if (ptnAlnum.matcher(ch).matches()){
18                                 if (status != 0){
19                                         pos = i;
20                                         status = 0;
21                                 }
22                         }
23                         else if (ptnAscii.matcher(ch).matches()){
24                                 if (status != 1){
25                                         pos = i;
26                                         status = 1;
27                                 }                               
28                         }
29                         else {
30                                 if (status != 2){
31                                         pos = i;
32                                         status = 2;
33                                 }                                                               
34                         }
35                 }
36                 return text.substring(0, pos);
37         }
38 }