我有一个像这样的文本文件,用“;”分隔 1022-3,1603-4,2012-5,2489-6;
要抓住“ - ”之前的第一部分并传递给变量,并与毫秒进行比较,如果等于数字,则捕获“ - ”之后的数字。 分号后面的下一个数字就这样做了,所以前面。
public static long MilliSeconds() {
// get Calendar instance
Calendar now = Calendar.getInstance();
return now.getTimeInMillis();
}
代码的开头要做我需要的东西
private void LerArquivo() {
String lstrNomeArq;
File arq;
String lstrlinha;
long tempoInicio = 0;
long tempoDecorrido = 0;
try {
tempoDecorrido = (RecordSound.MilliSeconds() - tempoInicio);
lstrNomeArq = "/Android/data/br.com.couldsys.drumspro/cache/GravaSound.TXT";
String conteudotexto = "";
arq = new File(Environment.getExternalStorageDirectory(),
lstrNomeArq);
BufferedReader br = new BufferedReader(new FileReader(arq));
// pega o conteudo do arquivo texto
conteudotexto = br.readLine();
String capturaIndex = ("Conteudo do texto: "
+ conteudotexto.substring(
conteudotexto.indexOf("-") + 1,
conteudotexto.indexOf(";",
conteudotexto.lastIndexOf("-"))));
if (tempoDecorrido == capturatempo) {
DrumsProActivity.vsm.playSound(capturaindex);
// ler a nova linha
// se chegar ao final do string então para o while
if (conteudotexto.length() > 0) {
executar = false;
}
}
} catch (Exception e) {
trace("Erro : " + e.getMessage());
}
}
答案 0 :(得分:2)
使用这个更简单的代码:创建一个子串数组,每个子串包含一个格式化的字符串#### - #
string[] MyStr = conteudotexto.split(',');
string sss= MyStr[0];
string sss2= MyStr[1];
...
现在sss是1022-3 sss2是1603-4等等......
然后重用split功能:
string[] MyStr2 = sss.split('-');
现在我们有: MyStr2 [0] = 1022 MyStr2 [1] = 3
答案 1 :(得分:2)
也许不是特别优雅但对我来说实用 - 使用String split方法。首先用“,”分开
String[] parts = conteudotexto.split(",");
然后与每个部分(这里是第一个)
String[] subParts = parts[0].split("-");
只需提供你需要看到的所有部分,并且没有危险与职位混在一起等。
答案 2 :(得分:0)
我现在需要知道如何捕捉部分文字。
arq = new File(Environment.getExternalStorageDirectory(),
lstrNomeArq);
BufferedReader br = new BufferedReader(new FileReader(arq));
// pega o conteudo do arquivo texto
conteudotexto = br.readLine();
文本可能会有所不同更多分离有两个,第一个数字与第二个数字分开一个“ - ”(破折号),然后用“,”(逗号)分隔
<强> 549-8,1019-9,1404-3,1764-3,2208-10,2593-5,2938-9,3264-6,3700-0,4174-7,4585-8, 4840-2,5192-9,5540-10,5932-0,
如图所示
String[] parte1 = conteudotexto.split(",");
e em seguida
String[] parte2 = parte1[0].split("-");
我要做的规则是:在方法内转一毫秒并与文本的第一部分进行比较
<强>型强>
* *如果valor_milissegundos第一部分等于文本数 ---&GT;输入并运行playSound函数(文本的第二个数字); ------&GT;转到文本循环中的下一个数字,捕获文本的第二个数字并将其与毫秒进行比较,如果相等则进入IF块并在破折号后捕获第二个数字,依此类推,直到达到结束时为止文本。** 强>
方法返回毫秒计算毫秒
public static long MilliSeconds() {
// get Calendar instance
Calendar now = Calendar.getInstance();
return now.getTimeInMillis();
}
我希望了解为什么我使用谷歌翻译
谢谢