Bry*_*can 0 c# string substring
我想,以将其存储在NO-SQL数据库拆分一个字符串对象,其中每列的存储容量不超过64KB以上,我的字符串是220 kb的.
我所做的是将我所拥有的任何字符串分割成段.我想把它分成4个像:
int howManyBytes = System.Text.ASCIIEncoding.ASCII.GetByteCount(serializedObject);
if (howManyBytes > 64000)
{
//divide by 4
int whereToSplit = Convert.ToInt32(howManyBytes * 0.25);
returnValue.MethodReturnValue = serializedObject.Substring(0, whereToSplit);
returnValue.MethodReturnValue2 = serializedObject.Substring(whereToSplit, whereToSplit * 2);
returnValue.MethodReturnValue3 = serializedObject.Substring(whereToSplit * 2, whereToSplit * 3);
returnValue.MethodReturnValue4 = serializedObject.Substring(whereToSplit * 3);
}
Run Code Online (Sandbox Code Playgroud)
我想要做的时候遇到问题returnValue.MethodReturnValue3 = serializedObject.Substring(whereToSplit * 2, whereToSplit * 3),除了索引超出范围之外.当我的字符串的长度是225358并且我的whereToSplit是56340时,会发生此问题.
知道为什么会这样吗?
String.Substring第二个参数是要返回的长度,而不是第二个分割位置.这不能超出字符串的结尾.因此,它应该是:
returnValue.MethodReturnValue = serializedObject.Substring(0, whereToSplit);
returnValue.MethodReturnValue2 = serializedObject.Substring(whereToSplit, whereToSplit); // No *2
returnValue.MethodReturnValue3 = serializedObject.Substring(whereToSplit * 2, whereToSplit); // No *3
returnValue.MethodReturnValue4 = serializedObject.Substring(whereToSplit * 3);
Run Code Online (Sandbox Code Playgroud)
此外,225358 * 0.25实际上是56339.5和越来越圆润起来.您可能需要考虑这一点,因为您的最后一个字符串最终会比其他字符串短.(但是,在您的方案中,这可能会或可能不重要.)
| 归档时间: |
|
| 查看次数: |
88 次 |
| 最近记录: |