0

Supoose I have input a line "MOVE R1, R2", I split the words with respect to white space and store them individually into an array token[] as follows:

String[] token = new String[0];// array initialization
FileInputStream fstream = new FileInputStream("a.txt");

BufferedReader br = new BufferedReader(new InputStreamReader(fstream));         
//Read file line by line and storing data in the form of tokens
While((strLine = br.readLine()) != null){
token = strLine.split(" ");// split w.r.t spaces                
}

So the elements at each index are as follows: token[0]=MOVE token[1]=R1, token[2]=R2

But what I want is as follows: token[0]=MOVE token[1]=1 token[2]=2

I want to store only the numerical values in the token[i] where i>0, trimming off R and comma(,). I m unable to figure out how to use relaceAll() with arrays. How can I do that? Thanks in advance.

4

3 に答える 3

5

このコードを試してください:

str = str.replaceAll("[^\\d.]", "");

これにより、数値以外のものは削除されます。

于 2013-03-28T16:56:06.433 に答える
0

トークン化する前に置き換えます。

while((strLine = br.readLine()) != null){
strLine = strLine.replaceAll("[^\\d]","");
token = strLine.split(" ");// split w.r.t spaces                
}
于 2013-03-28T17:21:14.500 に答える
0
s=s.replaceAll("\\D", ""); 

while ループ内の個々の分割文字列要素について..

于 2013-03-28T17:27:57.123 に答える