我有几行如下:
AAAAAABBBBDDDAAADDSSDD,FLAGE=TRUE,LLUUGJYFR
TTT:1
TTT:2
AAAAAABBBBDDDAAADDSSDD,FLAGE=TRUE,YYTTREETU,YTTREFF
TTT:1
AAAAAABBBBDDDAAADDSSDD,FLAGE=TRUE,KKIFRDEEW,YTRFHFFD
TTT:1
TTT:2
TTT:3
TTT:4
AAAAAABBBBDDDAAADDSSDD,FLAGE=FALSE,TYYRFFGGGFD,JJUUGGGGF
TTT:1
TTT:2
AAAAAABBBBDDDAAADDSSDD,FLAGE=TRUE,KKYGG,GGRFFFF,KUUYFFF
TTT:1
如果java中的FLAG = TRUE,我想计算TTT和FLAG = TRUE 我怎么检查呢? 谢谢你的帮助
答案 0 :(得分:3)
以下代码可能会对您有所帮助。
int res1 = countString(yourinput , "FLAGE=TRUE");
int res2 = countString(yourinput , "TTT");
int res3 = countTrueTTT(yourinput , "FLAGE" , "TTT")
public int countString(String yourString , String search){
int sLen = search.length;
int len = yourString.length;
int count = 0;
int index = yourString.indexOf(search, 0);
while(index > 0){
count++;
index = yourString.indexOf(search, index + sLen);
}
return count;
}
public int countTrueTTT(String yourString , String under , String search){
int len = yourString.length;
int sLen = under.length;
int count = 0;
int index = yourString.indexOf(under+"=TRUE" , 0);
int nextIndex = yourString.indexOf(under , index+sLen);
while(index > 0 && nextIndex > 0){
count += countString(yourString.subString(index,nextIndex),search);
index = yourString.indexOf(under+"=TRUE" , index+sLen);
nextIndex = yourString.indexOf(under , index+sLen);
if(nextIndex <0)
nextIndex = yourString.length;
}
return count;
}
答案 1 :(得分:2)
var lines = ["here", "are", "your", "lines"];
var count = 0;
for(i in lines) {
var line = lines[i];
if( /(^TTT:)|(FLAGE=TRUE)/.test(line) ) {
count++;
}
}
alert(count);
修改强>
抱歉,以为是javascript .. 但你明白了。使用正则表达式。