基于分隔符拆分字符串,并将拆分作为行插入表中

时间:2016-02-10 09:48:38

标签: c# sql sql-server

我有一个字符串如下:

L1N:0V L2N:0V L3N:0V L12:0V L23:0V L31:0V IL1:0A IL2:0A IL3:0A RPM:0 FREQ:0.00Hz P:0.00psi T:64'C Fuel:43% kVA:0 BVolt:27.00V Hrs:2272

我对以下字符串中的粗体分割感兴趣

  

L1N: 0 V L2N: 0 V L3N: 0 V L12: 0 V L23: 0 V L31: 0 V IL1: 0 A IL2: 0 A IL3: 0 A RPM:0 FREQ: 0.00 Hz P: 0.00 psi T: 64 'C燃料: 43 % kVA: 0 BVolt: 27.00 V小时: 2272

我想为每个分割创建多行,如下所示。

<table>
  <tr>
    <td>ID</td>
    <td>Value</td>
    </tr>
  <tr>
    <td>1</td>
    <td>0</td>
    </tr>
  <tr>
    <td>2</td>
    <td>0</td>
    </tr>
  <tr>
    <td>3</td>
    <td>0</td>
    </tr>
  <tr>
    <td>4</td>
    <td>0</td>
    </tr>
  <tr>
    <td>5</td>
    <td>0</td>
    </tr>
  <tr>
    <td>6</td>
    <td>0</td>
    </tr>
  <tr>
    <td>7</td>
    <td>0</td>
    </tr>
  <tr>
    <td>8</td>
    <td>0</td>
    </tr>
  <tr>
    <td>9</td>
    <td>0</td>
    </tr>
  <tr>
    <td>10</td>
    <td>0</td>
    </tr>
  <tr>
    <td>11</td>
    <td>0</td>
    </tr>
  <tr>
    <td>12</td>
    <td>0</td>
    </tr>
  <tr>
    <td>13</td>
    <td>64</td>
    </tr>
  <tr>
    <td>14</td>
    <td>43</td>
    </tr>
  <tr>
    <td>15</td>
    <td>0</td>
    </tr>
  <tr>
    <td>16</td>
    <td>27</td>
    </tr>
  <tr>
    <td>17</td>
    <td>2272</td>
    </tr>


  </table>

1 个答案:

答案 0 :(得分:0)

在同意上述评论的同时,请为初学者尝试:

string[] substring = myString.Split(':');
foreach (string str in substrings){
     if(!string.IsNullOrEmpty(str){
          var val = double.tryParse(str);
              }
          }

然后将这些值用于你的html(在javascript中就像是

var table = document.getElementById('myTable'); var row = table.insertRow(0); cell1 = row.insertCell(0); cell1.innerHTML = val;