在字符串中找到一对字符串或字母 [英] Find pair of strings or letters in the string

查看:99
本文介绍了在字符串中找到一对字符串或字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach (string word in arrayList)
{
    pair = " ";
    for (i = 0; i <= word.Length - 1; i++)
    {
        if (i == word.Length - 1)
        {
            if (i != 0)
            {
                if (word[i] == word[i - 1])
                {
                    pair += word[i];
                    Console.Write(pair + "," + " - " + word + "\n");
                    pair = null;
                }
            }
        }
        else
        {
            if (i + 1 <= word.Length - 1)
            {
                if (word[i] == word[i + 1])
                {
                    pair += word[i];
                }
                else
                {
                    if (i != 0)
                    {
                        if (word[i] == word[i - 1])
                        {
                            pair += word[i];
                            Console.Write(pair + "," + " - " + word + "\n");
                            pair = null;
                        }
                    }
                }
            }
        }
    }
}





我尝试上面的代码,

如果字符串是:成功就是我的生活输出是

cc, - 成功

ss, - 成功

但我希望输出是,

cc,ss, - 成功

请帮帮我,谢谢



I try above code,
if string is : "success is my life" the output is
cc, - success
ss, - success
but i expect output is,
cc,ss, - success
Please help me, thank you

推荐答案

沿着这条线的东西。请注意,这只会检查当前字符的直接字符。



Something along this line. Note this will only check for character immediate to the current one.

 string inp = "success is my life is success";
            string[] allWords = inp.Split(' ');

            var counts = allWords
    .GroupBy(w => w)
    .Select(g => new { Word = g.Key, Count = g.Count() })
    .ToList();

            var recurringWords = counts.Where(p => p.Count > 1).Select(p => p.Word).ToList();

            foreach (string recWord in recurringWords)
            {
                Console.WriteLine(recWord);
            }
/*Find character combinations*/
            List<string> resString = new List<string>();
            foreach (string thisWord in allWords)
            {
                char[] allChars = thisWord.ToCharArray();
                char[] charNext = allChars.Skip(1).ToArray();

                string combinations = "";
                
                for (int i = 0; i < allChars.Length-1; i++)
                {
                    if (allChars[i] == charNext[i])//combination found
                    {
                        combinations += allChars[i] + "" + allChars[i]+",";
                    }
                   
                }
                if (combinations!="")
                {
                    resString.Add(combinations + "-" + thisWord);
                }
                
            }
            foreach (string res in resString)
            {
                Console.WriteLine(res);
            }
            Console.ReadKey();</string></string>


尝试以下代码:

Try below code:
string inputStr = "success is my life";
string[] arrayList = inputStr.Split(' ');

string pair = "";
Dictionary<string,string> tempList = new Dictionary<string,string>();

foreach (string word in arrayList)
{
	pair = " ";
	for (int i = 0; i <= word.Length - 1; i++)
	{
		if (i == word.Length - 1)
		{
			if (i != 0)
			{
				if (word[i] == word[i - 1])
				{
					pair += word[i];

					if (tempList.ContainsKey(word.Trim()))
					{
						tempList[word.Trim()] = tempList[word.Trim()] + "," + pair;
					}
					else
					{
						tempList.Add(word.Trim(), pair.Trim());
					}
					//Console.Write(pair + "," + " - " + word + "\n");
					pair = null;
				}
			}
		}
		else
		{
			if (i + 1 <= word.Length - 1)
			{
				if (word[i] == word[i + 1])
				{
					pair += word[i];
				}
				else
				{
					if (i != 0)
					{
						if (word[i] == word[i - 1])
						{
							pair += word[i];
							

							if (tempList.ContainsKey(word.Trim()))
							{
								tempList[word.Trim()] = tempList[word.Trim()] + "," + pair;
							}
							else
							{
								tempList.Add(word.Trim(), pair.Trim());
							}

							//Console.Write(pair + "," + " - " + word + "\n");
							pair = null;
						}
					}
				}
			}
		}
	}
}

foreach (var temp in tempList)
{
	Console.Write(temp.Value + " - " + temp.Key + "\n");
}



注意:我只是根据需要部分修改了代码并实现了逻辑。


Note: I just modified partially your code and implemented logic as per your need.


这篇关于在字符串中找到一对字符串或字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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