2013-02-06 5 views
2

.txt 파일에서 값을 구분해야합니다. LineNumberReader를 만들고 .split ("\ t")를 사용하여 단어를 구분했지만 두 번째 마지막 값 (q 값) 만 필요합니다. .split()을 지정하는 옵션이 있습니까?x 탭 뒤에 문자열 분할

test_id gene_id gene locus sample_1 sample_2 status value_1 value_2 log2(fold_change) test_stat p_value q_value significant 
XLOC_000001 XLOC_000001 TC012951 ChLG10:20399-27664 naive BttO NOTEST 0 0.0498691 1.79769e+308 1.79769e+308 0.210754 1 no 

답변

1

당신은 당신이 추출하고 한 줄의 코드에서 원하는 문자열을 얻을하고자하는 열 후 분할을 중지하려면 String#split(String regex, int limit) 방법을 사용할 수 있습니다 내 .txt 파일입니다 :

String line = "A\tB\tC\tD\tE\tF"; // tab separated content 
    int column = 3; // specify the column you want (first is 1) 
    String content = line.split("\t", column + 1)[column - 1]; // get content 
    System.out.println(content); // prints C (3rd column) 
+0

고맙습니다. 완벽하게 작동합니다. 좋은 하루 되세요. – Mirar

1
String[] array = someString.split("\t"); 
String secondToLast = array[array.length - 2]; 
+0

이것은 _ 슬래시 t_에서 분할됩니다. on _tab_ – jlordo

+0

관심 값은 항상 같은 배열 위치에 있습니다. 이 값을 얻으려면 그다지 어렵지는 않지만 필요하지 않은 문자열을 만들어냅니다. 나는 문자열 출력을 줄이기위한 방법을 찾고 있었다. – Mirar

+0

@Mirar : 나의 대답을 보라. – jlordo

0

이 라인이 주어 :

XLOC_000001 XLOC_000001 TC012951 ChLG10:20399-27664 naive BttO NOTEST 0 0.0498691 1.79769e+308 1.79769e+308 0.210754 1 no 

당신은 1을 원하는 당신이 표현 사용할 수 있습니다

(끝에서 두 번째 요소) : 호출자가 지정할 수 있습니다

private static final Pattern pattern = Pattern.compile("(?:\t|^)([^\t]*?)\t[^\t]*?(?:\\n|$)"); 
public static final String getPenultimateElement(String line) { 
    Matcher m = pattern.matcher(line); 
    if(m.find()) 
     return m.group(1) 
    return null; // or throw exception. 
} 

또는 : 함수에 싸여

String s ="XLOC_000001 XLOC_000001 TC012951\tChLG10:20399-27664\tnaive\tBttO\tNOTEST\t0\t0.0498691\t1.79769e+308\t1.79769e+308\t0.210754\t1\tno"; 
Matcher m = Pattern.compile("(?:\t|^)([^\t]*?)\t[^\t]*?(?:\\n|$)").matcher(s); 
if(m.find()) 
    System.out.println(m.group(1)); 

또는를, 구분 기호 :

public static final String getPenultimateElement(String line, String separator) { 
    separator = Pattern.quote(separator); 
    Matcher m = Pattern.compile("(?:" separator + "|^)([^" + separator + "]*?)" + separator + "[^" + separator + "]*?(?:\\n|$)").matcher(line); 
    if(m.find()) 
     return m.group(1) 
    return null; // or throw exception. 
}