Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将长字符串拆分为固定数组项_C#_Algorithm - Fatal编程技术网

C# 如何将长字符串拆分为固定数组项

C# 如何将长字符串拆分为固定数组项,c#,algorithm,C#,Algorithm,我有一个dictionary对象IDictionary,它只包含以下项: 第1项、第2项和第3项。每个项目的最大长度仅为50个字符 else { currentLine += " " + word; } } lines.Add(currentLine); // the last unfinished line 然后我有一个单词列表list。我需要一个循环,它将遍历这些单词并从Item1开始将它们添加到字典中,但是在将其添加到字典中之前,需要检查长度。如果新

我有一个dictionary对象
IDictionary
,它只包含以下项: 第1项、第2项和第3项。每个项目的最大长度仅为50个字符

    else {
        currentLine += " " + word;   
    }
}
lines.Add(currentLine); // the last unfinished line
然后我有一个单词列表
list
。我需要一个循环,它将遍历这些单词并从Item1开始将它们添加到字典中,但是在将其添加到字典中之前,需要检查长度。如果新项目和当前项目的长度加在一起大于50个字符,则单词需要下移到下一行(在本例中为Item2)


最好的方法是什么

我不知道为什么这个问题被否决了这么多,但也许原因是你已经有了一个非常清晰的算法,所以获得C代码应该很简单。就目前而言,要么你真的没有经验,要么你真的很懒。我将假设前者

无论如何,让我们来看看需求

1) “然后我有一个单词列表。”你已经有了某种形式的这一行

List<string> words = GetListOfWords(); 
4) “如果新项和当前项的长度相加大于50个字符”-+1表示空格

    if (currentLine.Length + word.Length + 1 > 50) {
5) “然后单词需要下移到下一行”

6) “从第1项开始,通读单词并将其添加到词典中”--你没有很清楚地表达这一点。你的意思是你想把每个单词连到最后一行,除非它会使这一行超过50个字符

    else {
        currentLine += " " + word;   
    }
}
lines.Add(currentLine); // the last unfinished line
就这样


如果您确实需要它作为一个有3行的IDictionary,只需这样做

var dict = new Dictionary<string,string>();
for(int lineNum = 0; lineNum < 3; lineNum ++)
    dict["Address"+lineNum] = lineNume < lines.Length ? lines[lineNum] : "";
var dict=newdictionary();
对于(int-lineNum=0;lineNum<3;lineNum++)
dict[“地址”+lineNum]=lineNume
我不知道为什么这个问题被否决了这么多,但也许原因是你已经有了一个非常清晰的算法,所以获得C代码应该很简单。就目前而言,要么你真的没有经验,要么你真的很懒。我将假设前者

无论如何,让我们来看看需求

1) “然后我有一个单词列表。”你已经有了某种形式的这一行

List<string> words = GetListOfWords(); 
4) “如果新项和当前项的长度相加大于50个字符”-+1表示空格

    if (currentLine.Length + word.Length + 1 > 50) {
5) “然后单词需要下移到下一行”

6) “从第1项开始,通读单词并将其添加到词典中”--你没有很清楚地表达这一点。你的意思是你想把每个单词连到最后一行,除非它会使这一行超过50个字符

    else {
        currentLine += " " + word;   
    }
}
lines.Add(currentLine); // the last unfinished line
就这样


如果您确实需要它作为一个有3行的IDictionary,只需这样做

var dict = new Dictionary<string,string>();
for(int lineNum = 0; lineNum < 3; lineNum ++)
    dict["Address"+lineNum] = lineNume < lines.Length ? lines[lineNum] : "";
var dict=newdictionary();
对于(int-lineNum=0;lineNum<3;lineNum++)
dict[“地址”+lineNum]=lineNume
我现在有这个,想知道是否有更好的解决方案:


public IDictionary AddWordsToDictionary(IList words)
{
    IDictionary addressParts = new Dictionary();
    addressParts.Add("Address1", string.Empty);
    addressParts.Add("Address2", string.Empty);
    addressParts.Add("Address3", string.Empty);

    int currentIndex = 1;
    foreach (string word in words)
    {
        if (!string.IsNullOrEmpty(word))
        {
            string key = string.Concat("Address", currentIndex);
            int space = 0;
            string spaceChar = string.Empty;
            if (!string.IsNullOrEmpty(addressParts[key]))
            {
                space = 1;
                spaceChar = " ";
            }
            if (word.Length + space + addressParts[key].Length > MaxAddressLineLength)
            {
                currentIndex++;
                key = string.Concat("Address", currentIndex);
                space = 0;
                spaceChar = string.Empty;
                if (currentIndex > addressParts.Count)
                {
                    break; // To big for all 3 elements so discard
                }
            }

            addressParts[key] = string.Concat(addressParts[key], spaceChar, word);
        }
    }

    return addressParts;
}

我目前有这个问题,想知道是否有更好的解决方案:


public IDictionary AddWordsToDictionary(IList words)
{
    IDictionary addressParts = new Dictionary();
    addressParts.Add("Address1", string.Empty);
    addressParts.Add("Address2", string.Empty);
    addressParts.Add("Address3", string.Empty);

    int currentIndex = 1;
    foreach (string word in words)
    {
        if (!string.IsNullOrEmpty(word))
        {
            string key = string.Concat("Address", currentIndex);
            int space = 0;
            string spaceChar = string.Empty;
            if (!string.IsNullOrEmpty(addressParts[key]))
            {
                space = 1;
                spaceChar = " ";
            }
            if (word.Length + space + addressParts[key].Length > MaxAddressLineLength)
            {
                currentIndex++;
                key = string.Concat("Address", currentIndex);
                space = 0;
                spaceChar = string.Empty;
                if (currentIndex > addressParts.Count)
                {
                    break; // To big for all 3 elements so discard
                }
            }

            addressParts[key] = string.Concat(addressParts[key], spaceChar, word);
        }
    }

    return addressParts;
}

我只想做下面的事情,然后用结果“行”做你喜欢的事情

静态列表构建行(列表字,int-lineLen)
{
列表行=新列表();
string line=string.Empty;
foreach(单词中的字符串)
{
如果(string.IsNullOrEmpty(word))继续;

如果(line.Length+word.Length+1,我将只执行以下操作,并对生成的“行”执行您喜欢的操作

静态列表构建行(列表字,int-lineLen)
{
列表行=新列表();
string line=string.Empty;
foreach(单词中的字符串)
{
如果(string.IsNullOrEmpty(word))继续;

如果(line.Length+word.Length+1那么你想让我们写你的算法和代码?添加了标记,但你要求的是插入硬词包装的算法。我只需要一段代码示例。那么你想让我们写你的算法和代码?添加了标记,但你要求的是插入硬词包装的算法。我只需要一段代码示例。