OSDN Git Service

StringUtility
[traindelaybot/source.git] / workspace / .metadata / .plugins / org.eclipse.core.resources / .history / e6 / 205b432bcd6a0011194396142d5d2649
diff --git a/workspace/.metadata/.plugins/org.eclipse.core.resources/.history/e6/205b432bcd6a0011194396142d5d2649 b/workspace/.metadata/.plugins/org.eclipse.core.resources/.history/e6/205b432bcd6a0011194396142d5d2649
new file mode 100644 (file)
index 0000000..304bfdc
--- /dev/null
@@ -0,0 +1,40 @@
+package com.yuji.tdb.utility;
+
+import java.util.regex.Pattern;
+
+public class StringUtil {
+       private static Pattern ptnAlnum = Pattern.compile("[a-zA-Z0-9]");
+       private static Pattern ptnAscii = Pattern.compile("\\p{ASCII}");
+       
+       public static String parseSubstring(String text, int length){
+               int len = text.length();
+               int status = -1;
+               int pos = 0;
+               
+               for (int i = 0; i < len + 1 && i < length + 1; i++){
+                       if (i >= len){
+                               pos = i;
+                               break;
+                       }
+                       String ch = text.substring(i, i + 1);
+                       
+                       if (ptnAlnum.matcher(ch).matches()){
+                               if (status != 0){
+                                       pos = i;
+                                       status = 0;
+                               }
+                       }
+                       else if (ptnAscii.matcher(ch).matches()){
+                               if (status != 1){
+                                       pos = i;
+                                       status = 1;
+                               }                               
+                       }
+                       else {
+                               pos = i;
+                               status = 2;
+                       }
+               }
+               return text.substring(0, pos);
+       }
+}