gpt4 book ai didi

c# - 在可变数量的字符后拆分字符串

转载 作者:行者123 更新时间:2023-12-02 05:01:44 25 4
gpt4 key购买 nike

我想在可变数量的字符之后拆分名为:invoerstring 的字符串(n 是字符串需要拆分时的字符数)。 如果字符串长度比变量 n 短,则需要添加空格直到字符串长度 = n。结果需要显示在名为 uitvoer 的文本字段中。

到目前为止是这样的:

string invoerstring = invoer.Text;

if (invoerstring.Length < n)
{
invoerstring += "";
char[] myStrChars = invoerstring.ToCharArray();
}

if (invoerstring.Length == n)
{
string [] blok = invoerstring.Split();
foreach (string word in blok)
{
uitvoer.Text = word;
}
}

编辑:上面给出的解决方案并不能完全为我完成工作,也许对我发布练习有帮助:

|| crypt n m d text || text is padded with spaces until its length is a multiple of n || the characters in text are circulary shifted in the alphabet by the displacement d || example: if d = 1 then 'a' -> 'b' , 'b' -> 'c' .... etc... 'z' -> 'a' || text is divided in blocks of length n characters || inside every block of n the characters are circulary shifted m times to the left || the shifted groups are concatenated

我已经解出了m,d只要解出n就可以了。


上面给出的解决方案并不能完全满足我的要求,也许对我发布练习有帮助:

|| crypt n m d 文本||文本用空格填充,直到它的长度是 n 的倍数||文本中的字符在字母表中循环移动位移 d||示例:如果 d = 1,则 'a' -> 'b' , 'b' -> 'c' .... 等等... 'z' -> 'a'||文本被分成长度为 n 个字符的 block ||在 n 的每个 block 中,字符向左循环移动 m 次||移动的组连接在一起

我已经解出了m,d只要解出n就可以了。

最佳答案

这是你想要的代码,不需要遍历字符数组:

public static string EnsureExactLength(this string s, int length)
{
if (s == null)
throw new ArgumentNullException("null");

return s.PadRight(length).Substring(0, length);
}

你可以这样调用它:

string s = "Test string".EnsureExactLength(4); // will return "Test"
string s = "Te".EnsureExactLength(4); // will return "Te "

你可以找到一个例子LINQPad程序 here .

关于c# - 在可变数量的字符后拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16963484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com