与其他在字符串替换的东西烧焦重复 [英] Replace repeated char with something else in a string

查看:101
本文介绍了与其他在字符串替换的东西烧焦重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助找出一个逻辑:



所以,让我们说我有一个字符串,每当有字符重复内部的字符串,我需要更换它(字符 + 若干序列



例如:



原始字符串:abcdefgabfabc



期望的输出:abcdefga2b2f2a3b3c2



< BLOCKQUOTE>

A出现3次,所以第一个'一'仍是'A',但第二个'一'变成'A2',第三'一个'变成'A3'和相同的进到其它字符像b,B2,B3 ...




解决方案

  VAR海峡=abcdefgabfabc; 
VAR字符= str.Select((C,指数)=>
{
诠释计数= str.Substring(0,指数).Count之间(X =以及c ==点¯x );
如果(计数大于0)返回c.ToString()+(计数+ 1);
,否则返回c.ToString();
})的SelectMany(C => ; C).ToArray();
VAR的结果=新的字符串(字符); // abcdefga2b2f2a3b3c2


I need help to figure out a logic:

So, let's say I have a string, and whenever there is a Char repeated inside that string, I need to replace it with a (Char + sequence of number).

For example:

Original String: "abcdefgabfabc"

Expected output: "abcdefga2b2f2a3b3c2"

'a' occurs 3 times, so the first 'a' remains as 'a', but the second 'a' becomes 'a2', and the third 'a' becomes 'a3' and the same goes to other chars like b, b2, b3...

解决方案

var str = "abcdefgabfabc";
var chars = str.Select((c, index) =>
        {
            int count = str.Substring(0, index).Count(x => c == x);
            if (count > 0) return c.ToString() + (count+1);
            else return c.ToString();
        }).SelectMany(c => c).ToArray();
var result = new string(chars); // abcdefga2b2f2a3b3c2

这篇关于与其他在字符串替换的东西烧焦重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆