将较长的 word/string 切成较小的块以适合字段
Cut a longer word/string in smaller chunks for fitting a field
我想用一串很多词来填充一个字段。当我有很多词时,我需要一个来拆分这个词。如果我的单词超过 10 个字母,我需要在该单词的第 5 位添加 space。
如何使用更短的单词获取新的 "myString"?
static void Main(string[] args)
{
var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
var myStringSplit = myString.Split();
var query = myStringSplit.Where(x => x.Length > 10).ToList();
foreach (var item in query)
{
item.Insert(5, " ");
}
}
编辑: 我的预期结果:myString = "This is my strin g with some loooo ooooo ooooo ng words. Pleas e devid e meeee eeeee eeeee eeeee eeeee e"
;
试试这个可能会有帮助
static void Main(string[] args)
{
var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
var myStringSplit = myString.Split();
var query = myStringSplit.Where(x => x.Length > 10).ToList();
foreach (var item in query)
{
string outString = Regex.Replace(item, ".{5}", "[=10=] ");
}
}
这将在每五个单词后分隔字符串..
已编辑
static void Main(string[] args)
{
var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
var newString = string.Empty;
var myStringSplit = myString.Split();
var query = myStringSplit.Where(x => x.Length > 10).ToList();
foreach (var item in myStringSplit)
{
if(item.Length > 10)
{
var itemToSplit = item;
itemToSplit.Insert(5, " ");
newString += itemToSplit;
}
else
{
newString += item;
}
}
}
应该是这样的。您的尝试无效的原因是因为不允许您编辑 foreach 循环中的项目。 More info on the foreach loop
以下是如何获得结果的示例:
public static void Main(string[] args)
{
var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
string[] myStringSplit = myString.Split();
for (int i =0;i<myStringSplit.Length;i++)
{
if(myStringSplit[i].Length>5)
myStringSplit[i] = Regex.Replace(myStringSplit[i], ".{5}(?=.)", "[=10=] ");
}
var resultString = myStringSplit.Aggregate((x,y)=> x+" "+y);
Console.WriteLine(resultString);
}
输出如下:
This is my strin g with some loooo ooooo ooooo ng words . Pleas e
devid e meeee eeeee eeeee eeeee eeeee e
我想用一串很多词来填充一个字段。当我有很多词时,我需要一个来拆分这个词。如果我的单词超过 10 个字母,我需要在该单词的第 5 位添加 space。
如何使用更短的单词获取新的 "myString"?
static void Main(string[] args)
{
var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
var myStringSplit = myString.Split();
var query = myStringSplit.Where(x => x.Length > 10).ToList();
foreach (var item in query)
{
item.Insert(5, " ");
}
}
编辑: 我的预期结果:myString = "This is my strin g with some loooo ooooo ooooo ng words. Pleas e devid e meeee eeeee eeeee eeeee eeeee e"
;
试试这个可能会有帮助
static void Main(string[] args)
{
var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
var myStringSplit = myString.Split();
var query = myStringSplit.Where(x => x.Length > 10).ToList();
foreach (var item in query)
{
string outString = Regex.Replace(item, ".{5}", "[=10=] ");
}
}
这将在每五个单词后分隔字符串..
已编辑
static void Main(string[] args)
{
var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
var newString = string.Empty;
var myStringSplit = myString.Split();
var query = myStringSplit.Where(x => x.Length > 10).ToList();
foreach (var item in myStringSplit)
{
if(item.Length > 10)
{
var itemToSplit = item;
itemToSplit.Insert(5, " ");
newString += itemToSplit;
}
else
{
newString += item;
}
}
}
应该是这样的。您的尝试无效的原因是因为不允许您编辑 foreach 循环中的项目。 More info on the foreach loop
以下是如何获得结果的示例:
public static void Main(string[] args)
{
var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
string[] myStringSplit = myString.Split();
for (int i =0;i<myStringSplit.Length;i++)
{
if(myStringSplit[i].Length>5)
myStringSplit[i] = Regex.Replace(myStringSplit[i], ".{5}(?=.)", "[=10=] ");
}
var resultString = myStringSplit.Aggregate((x,y)=> x+" "+y);
Console.WriteLine(resultString);
}
输出如下:
This is my strin g with some loooo ooooo ooooo ng words . Pleas e devid e meeee eeeee eeeee eeeee eeeee e