【问题标题】:How to read a string and get values into an array?如何读取字符串并将值放入数组?
【发布时间】:2015-10-08 13:46:43
【问题描述】:

我想读取一个字符串:

TRANS = 2'b00 到 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]

按照与上述相同的顺序。

【问题讨论】:

  • 2'b012'b10 是从哪里来的?
  • 到目前为止你尝试了什么?我猜对了2'b 之后的数字是二进制数吗?
  • 这些是二进制数字值,在电子学中是这样写的。其中 00, 01, 10, 11 是增量值。
  • 我不知道如何读取字符串并获取这些递增的值。我被困在这里,请帮忙!
  • @npinti 他们来自b'00 to b'11。它们不需要出现在原始字符串中,它是一个范围

标签: java arrays numbers sequence


【解决方案1】:

你可以去掉开头和结尾。使用 Integer 类在十进制和二进制之间进行转换。示例

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]
        }
    }
}

【讨论】:

    【解决方案2】:

    这样做并不是很简单,因为有很多情况,例如,如果您有:

    "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
    

    【讨论】:

      【解决方案3】:

      这里还有一个示例代码:

          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]);
      

      【讨论】:

        猜你喜欢
        • 2018-12-14
        • 1970-01-01
        • 2013-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多