当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Build strings based on maximum length

问题描述:

Using a while loop I am iterating through string values stored in a database -

 while (x.Read())

{

value = (string)(x["ROWVALUE"]);

valueString += value ;

if (valueString.Length > 300)

{

valueString= valueString.Replace(value , "");

}

}

valueString is not allowed to be over 300 characters in length, so when that buffer is hit, I am removing the last addition of value to keep it under 300.

My problem being, once I have reached the maximum length for valueString, I need to create a next valueString for the remaining values and repeat this process until all the values have been retrieved. How could I achieve this?

网友答案:

You can store them in a list for example:

List<string> valueStrings = new List<string>();
string value, valueString;
while (x.Read())
{
    value = (string)(x["ROWVALUE"]);    

    if (valueString.Length + value.Length > 300)
    {
        valueStrings.Add(valueString);
        valueString = value;
    }
    else
    {
        valueString += value;
    }
}
valueStrings.Add(valueString); //don't forget about the last one
分享给朋友:
您可能感兴趣的文章:
随机阅读: