코드비교

Old VersionNew Version
12System.out.println(tokens); 12System.out.println(tokens); 
13} 13} 
14} 14} 
15public static String split(String str, String reg) { 15public static String split(String str, String delimiter) {
16Pattern p = Pattern.compile(reg); 16String result="";
17Matcher m = p.matcher(str); 17Pattern pattern = Pattern.compile(delimiter);
18String orig = str; 18String tmp= str;
19Matcher k = p.matcher(orig); 19Matcher matcher = pattern.matcher(str);
20String res = ""; 20Matcher matchertmp = pattern.matcher(tmp);
21int cnt = 0; 21int i=0; int cnt=0;
22while(k.find()) { 22while(matchertmp.find()) {
23cnt++; 23cnt++; 
24} 24} 
25String sv[] = new String[2*cnt+1]; 25//cnt가...0이면...
26for(int i=0 ; i<2*cnt+1 ; i++) { 26if(cnt==0) {
27sv[i] = ""; 27return str;
28} 28} 
29if(cnt == 0) { 29int[] start = new int[cnt];
30res = str; 30int[] end = new int[cnt];
   31while(matcher.find()) {
   32start[i] = matcher.start();
   33end[i] = matcher.end();
   34i++;
31} 35} 
32else { 32////
33int idc[] = new int[cnt*2]; 33String first="";
34int i=0; 34if(start[0]!=0) {
35while(m.find()) { 35first = str.substring(0,start[0]);//substring은 뒤의 것하나 전 인덱스 까지니까
36idc[i] = m.start();   
37idc[i+1] = m.end();   
38i+=2;   
39} 40} 
40i = 0; 41// String first = str.substring(0,start[0]);
41sv[0] += str.substring(0, idc[0]); 42//           String third ="";
42sv[0] += ","; 43//           if(end[cnt-1]!=str.length()-1) {
43sv[2*cnt] = str.substring(idc[cnt*2-1]); 44//               str.substring(end[cnt-1],str.length());
44if(idc[cnt*2-1] == str.length() + 1) { 45//           }
45sv[cnt*2] += ","; 46String third = str.substring(end[cnt-1],str.length());
   47String[] keep = new String[cnt];
   48String keepTmp="";
   49for(int k=0;k<cnt;k++) {
   50keep[k]=str.substring(start[k],end[k]);
46} 51} 
47res += sv[0]; 47String[] second = new String[cnt-1];
48for(i=1 ; i<cnt*2 ; i++) { 48for(int j=0;j<cnt-1;j++) {//가운데 낀 갯수는 cnt-1개
49sv[i] = str.substring(idc[i-1], idc[i]) + ","; 49second[j]=str.substring(end[j],start[j+1]);
50res += sv[i];   
51} 55} 
52res += sv[cnt*2]; 56result+=first;
   57for(int l =0;l<cnt;l++) {
   58result+=",";
   59result+=keep[l];
   60result+=",";
   61if(l==cnt-1)break;
   62result+=second[l];
53} 63} 
54return res; 64result+=third;
   65return result;
55} 66} 
56} 67} 

sample code

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
String str = sc.next();
String delimiter = sc.next();
String tokens = split(str, delimiter);
System.out.println(tokens);
}
}
}
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
String str = sc.next();
String delimiter = sc.next();
String tokens = split(str, delimiter);
System.out.println(tokens);
}
}
public static String split(String str, String reg) {
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
String orig = str;
Matcher k = p.matcher(orig);
String res = "";
int cnt = 0;
while(k.find()) {
cnt++;
}
String sv[] = new String[2*cnt+1];
for(int i=0 ; i<2*cnt+1 ; i++) {
sv[i] = "";
}
if(cnt == 0) {
res = str;
}
else {
int idc[] = new int[cnt*2];
int i=0;
while(m.find()) {
idc[i] = m.start();
idc[i+1] = m.end();
i+=2;
}
i = 0;
sv[0] += str.substring(0, idc[0]);
sv[0] += ",";
sv[2*cnt] = str.substring(idc[cnt*2-1]);
if(idc[cnt*2-1] == str.length() + 1) {
sv[cnt*2] += ",";
}
res += sv[0];
for(i=1 ; i<cnt*2 ; i++) {
sv[i] = str.substring(idc[i-1], idc[i]) + ",";
res += sv[i];
}
res += sv[cnt*2];
}
return res;
}
}
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
String str = sc.next();
String delimiter = sc.next();
String tokens = split(str, delimiter);
System.out.println(tokens);
}
}
public static String split(String str, String delimiter) {
String result="";
Pattern pattern = Pattern.compile(delimiter);
String tmp= str;
Matcher matcher = pattern.matcher(str);
Matcher matchertmp = pattern.matcher(tmp);
int i=0; int cnt=0;
while(matchertmp.find()) {
cnt++;
}
//cnt가...0이면...
if(cnt==0) {
return str;
}
int[] start = new int[cnt];
int[] end = new int[cnt];
while(matcher.find()) {
start[i] = matcher.start();
end[i] = matcher.end();
i++;
}
////
String first="";
if(start[0]!=0) {
first = str.substring(0,start[0]);//substring은 뒤의 것하나 전 인덱스 까지니까
}
// String first = str.substring(0,start[0]);
//            String third ="";
//            if(end[cnt-1]!=str.length()-1) {
//            	str.substring(end[cnt-1],str.length());
//            }
String third = str.substring(end[cnt-1],str.length());
String[] keep = new String[cnt];
String keepTmp="";
for(int k=0;k<cnt;k++) {
keep[k]=str.substring(start[k],end[k]);
}
String[] second = new String[cnt-1];
for(int j=0;j<cnt-1;j++) {//가운데 낀 갯수는 cnt-1개
second[j]=str.substring(end[j],start[j+1]);
}
result+=first;
for(int l =0;l<cnt;l++) {
result+=",";
result+=keep[l];
result+=",";
if(l==cnt-1)break;
result+=second[l];
}
result+=third;
return result;
}
}