修改字符串并添加新字符串

时间:2016-01-18 07:28:40

标签: c# string c#-4.0

我有字符串值740D1E11625C89019E61D0CC我想插入" - "在每个计数5长度后的字符串中。

我正在使用

Array print in order of count and index:

$temp = array();

foreach($A as $b){
    foreach($b as $c){
        if(isset($tmep[$c])){
            $tmep[$c]++;
        }else{
            $tmep[$c] = 1;
        }
    }
}

function SortArrayByKeyThanValue (&$pArray, $pSortMethodForKey = SORT_ASC, $pSortMethodForValue = SORT_DESC){
    # check user input: sorting is not necessary
    if (count($pArray) < 2)
        return;

    # define $k and $v as array_multisort() needs real variables, as user input is put by reference
    $k = array_keys  ($pArray);
    $v = array_values($pArray);

    array_multisort(
        $v, $pSortMethodForValue,
        $k, $pSortMethodForKey
    );
    $pArray = array_combine($k, $v);
}

SortArrayByKeyThanValue($tmep); 

$B = array();
array_walk($tmep, function($occurances, $value) use (&$B){
    for($i=0;$i<$occurances;$i++) $B[] = $value;
});

echo implode(',', $B);

结果

740D1-E11625C89019E61D0CC

应该是

740D1-E1162-5C890-19E61-D0CC

这是修改字符串的正确方法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用Batch中的MoreLINQ。它也有一个Nuget package

var test = decodeHtml("&lt;!-- SC_OFF --&gt;&lt;div class=\"md\"&gt;&lt;p&gt;Here is an &lt;a href=\"http://example.com\"&gt;example link&lt;/a&gt;&lt;/p&gt;\n\n&lt;p&gt;Here is some body text for you with &lt;sup&gt;superscript&lt;/sup&gt; and a bit of &lt;strong&gt;boldness&lt;/strong&gt; and some &lt;em&gt;italics&lt;/em&gt;. I forgot to mention this regular link here &lt;a href=\"http://example.com\"&gt;http://example.com&lt;/a&gt; so don&amp;#39;t forget that one. sometimes there can be more than one &lt;a href=\"http://example.com\"&gt;formatted link&lt;/a&gt; and &lt;a href=\"http://example.com\"&gt;http://example.com&lt;/a&gt;&lt;/p&gt;\n\n&lt;ul&gt;\n&lt;li&gt;so there you go&lt;/li&gt;\n&lt;li&gt;some sample text to work with&lt;/li&gt;\n&lt;/ul&gt;\n\n&lt;p&gt;&lt;del&gt;strikethrough&lt;/del&gt;&lt;/p&gt;\n\n&lt;pre&gt;&lt;code&gt;if(canCode){\n    //lol dont kid yourself\n}\n&lt;/code&gt;&lt;/pre&gt;\n&lt;/div&gt;&lt;!-- SC_ON --&gt;")

$(document.body).append(test);

var $aTags = $('div.md').find('a');

$aTags.each(function() {
   var $aTag = $(this),
       href = $aTag.attr('href');
   $aTag.on('click',function(e) {
     myfunc(href);
     e.preventDefault();
   }); 
});

function myfunc(href) {
  alert(href);
}

function decodeHtml(html) {
   var txt = document.createElement("textarea");
   txt.innerHTML = html;
   return txt.value;
}

打印

var s = "740D1E11625C89019E61D0CC";
var array = s.Batch(5, seq => new string(seq.ToArray()));
Console.WriteLine(string.Join("-", array));