가끔 특정값을 다른값으로 바꿔야 할 경우가 있다.
예를 들어 물리드라이브로 값이 있고, 이걸 ftp주소로 변환한다고 한다면??
e:\j-drive\한국인.hwp -> ftp://127.0.1:8080/jd/한국인.hwp
e:\k-drive\정꾸.gif -> ftp://127.0.1:8080/kd/정꾸.gif
h:\n-drive\정선생.mp3 -> ftp://127.0.1:8080/hd/정선생.mp3
e:\k-drive\정꾸.gif -> ftp://127.0.1:8080/kd/정꾸.gif
h:\n-drive\정선생.mp3 -> ftp://127.0.1:8080/hd/정선생.mp3
알고는 있으나 이런 경우 써먹지 않았던... split..
이럴때 쓰면 참 편하다. 바보같이 이런 데이터성 작업을 할때
String [] realpath = {"e:\\j-drive\\","e:\\k-drive\\","h:\\n-drive\\"};
이런 따옴표 노가다를 했었다니... 아오... (split함수를 모르는것도 아니고)
public static String getFtpurl(String str)
{
String result = str;
String [] realpath = "e:\\j-drive\\,e:\\k-drive\\,h:\\n-drive\\".Split(',');
String [] ftppath = "ftp://127.0.0.1:8080/jd/,ftp://127.0.0.1:8080/kd/,ftp://127.0.0.1:8080/hd/".Split(',');
for (int i=0; i<(realpath==null?0:realpath.Length); i++)
{
if ( i >= (ftppath==null?0:ftppath.Length))
break; //두번째 배열보다 크면 탈출 (운나쁘게 index넘는 오류방지)
result = result.Replace(realpath[i], ftppath[i];
}
return result;
}
{
String result = str;
String [] realpath = "e:\\j-drive\\,e:\\k-drive\\,h:\\n-drive\\".Split(',');
String [] ftppath = "ftp://127.0.0.1:8080/jd/,ftp://127.0.0.1:8080/kd/,ftp://127.0.0.1:8080/hd/".Split(',');
for (int i=0; i<(realpath==null?0:realpath.Length); i++)
{
if ( i >= (ftppath==null?0:ftppath.Length))
break; //두번째 배열보다 크면 탈출 (운나쁘게 index넘는 오류방지)
result = result.Replace(realpath[i], ftppath[i];
}
return result;
}
간단한 팁이지만...
지금까지 왜 응용할 생각을 못했을까...