如何读取字符串并将值放入数组?
How to read a string and get values into an array?
我想读取一个字符串:
TRANS = 2'b00 to 2'b11
并将值 2'b00、2'b01、2'b10、2'b11 提取到数组中。如何用最简单的方式做到这一点?
对于:
String str = "TRANS = 2'b00 to 2'b11";
输出应该是:
[2'b00, 2'b01, 2'b10, 2'b11]
与上述顺序相同。
这不是很简单,因为有很多情况,例如,如果您有:
"TRANS = 2'b00 to 3'c11" //you would not have the good results.
这里有一个拆分字符串的例子:(它只是为了学习,对生产程序来说不是很好):
String str = "TRANS = 2'b00 to 2'b11";
String[] parts = str.split("=");
String[] values= parts[1].split("to"); //part[1] is the right part after char =
int size = values.length;
String val_from = values[0].trim().split('b');
String val_to = values[1].trim().split('b');
String[] binary_values= {"00","01","10", "11"};
int start_count = 0;
for(i=0; i<4; i++){
if(val_from[1]==binary_values[i]){
start_count = i;
break;
}
}
ArrayList<String> result = new ArrayList<String>();
for(i=start_count ; i<4; i++){
result.add(val_from[0]+binary_values[i]);
}
//here the new values are stocked in : result
你可以去掉开头和结尾。使用 Integer class 在十进制和二进制之间进行转换。例子
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Seq {
public static void main(String[] args) {
String str = "TRANS = 2'b00 to 2'b11";
Matcher matcher = Pattern.compile("2'b\s*(\w+)").matcher(str);
List<String> range = new ArrayList<String>();
while (matcher.find()) {
range.add(matcher.group(1));
}
if(range.size() == 2){
List<String> output = new ArrayList<String>();
int start = Integer.parseInt(range.get(0),2);
int end = Integer.parseInt(range.get(1),2);
for(int i=start; i<=end; i++){
output.add("2'b" + String.format("%02d", Integer.parseInt(Integer.toBinaryString(i))));
}
System.out.println(output); // Prints [2'b00, 2'b01, 2'b10, 2'b11]
}
}
}
这里还有一个示例代码:
String s = "TRANS = 2'b00 to 2'b11";
//get max and min values, 00 and 11 here;
int max_no=Integer.parseInt(s.substring(s.length()-2, s.length()));
int min_no=Integer.parseInt(s.substring(s.indexOf("b", 1)+1,s.indexOf("b", 1)+3));
String [] output = new String[max_no - min_no +1];
for(int i=min_no; i<=max_no; i++){
output[i] = "2'b"+ (i <10 ? "0" : "")+i;
}
for(int i=0; i< output.length ;i++){
System.out.println(output[i]);
我想读取一个字符串:
TRANS = 2'b00 to 2'b11
并将值 2'b00、2'b01、2'b10、2'b11 提取到数组中。如何用最简单的方式做到这一点?
对于:
String str = "TRANS = 2'b00 to 2'b11";
输出应该是:
[2'b00, 2'b01, 2'b10, 2'b11]
与上述顺序相同。
这不是很简单,因为有很多情况,例如,如果您有:
"TRANS = 2'b00 to 3'c11" //you would not have the good results.
这里有一个拆分字符串的例子:(它只是为了学习,对生产程序来说不是很好):
String str = "TRANS = 2'b00 to 2'b11";
String[] parts = str.split("=");
String[] values= parts[1].split("to"); //part[1] is the right part after char =
int size = values.length;
String val_from = values[0].trim().split('b');
String val_to = values[1].trim().split('b');
String[] binary_values= {"00","01","10", "11"};
int start_count = 0;
for(i=0; i<4; i++){
if(val_from[1]==binary_values[i]){
start_count = i;
break;
}
}
ArrayList<String> result = new ArrayList<String>();
for(i=start_count ; i<4; i++){
result.add(val_from[0]+binary_values[i]);
}
//here the new values are stocked in : result
你可以去掉开头和结尾。使用 Integer class 在十进制和二进制之间进行转换。例子
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Seq {
public static void main(String[] args) {
String str = "TRANS = 2'b00 to 2'b11";
Matcher matcher = Pattern.compile("2'b\s*(\w+)").matcher(str);
List<String> range = new ArrayList<String>();
while (matcher.find()) {
range.add(matcher.group(1));
}
if(range.size() == 2){
List<String> output = new ArrayList<String>();
int start = Integer.parseInt(range.get(0),2);
int end = Integer.parseInt(range.get(1),2);
for(int i=start; i<=end; i++){
output.add("2'b" + String.format("%02d", Integer.parseInt(Integer.toBinaryString(i))));
}
System.out.println(output); // Prints [2'b00, 2'b01, 2'b10, 2'b11]
}
}
}
这里还有一个示例代码:
String s = "TRANS = 2'b00 to 2'b11";
//get max and min values, 00 and 11 here;
int max_no=Integer.parseInt(s.substring(s.length()-2, s.length()));
int min_no=Integer.parseInt(s.substring(s.indexOf("b", 1)+1,s.indexOf("b", 1)+3));
String [] output = new String[max_no - min_no +1];
for(int i=min_no; i<=max_no; i++){
output[i] = "2'b"+ (i <10 ? "0" : "")+i;
}
for(int i=0; i< output.length ;i++){
System.out.println(output[i]);