![](/img/trans.png)
[英]How to replace a string with regular expression looking for ':' and variable white space?
[英]Replace each white space in string with number of whitespaces
我有以下挑戰。 我需要用一個數字替換字符串中的每個空格,該數字表示有多少空格。 例如:“你好世界!”; 應修改為“Hello1World2!”; 我如何在 c# 中實現這一點。 請幫忙
我終於得到了正則表達式的答案:使用正則表達式的MatchEvaluator
類:
using System.Text.RegularExpressions;
static void Main(string[] args)
{
string input = "bla absl ael dls ale ";
var result2 = Regex.Replace(input, "\s+", new MatchEvaluator(ReplaceByCount));
Console.WriteLine(result2);
//returns bla1absl1ael3dls1ale1
}
private static string ReplaceByCount(Match m)
{
return m.Value.Length.ToString();
}
編輯:用“\\s+”替換“+”以匹配@Marco-Forbergs 評論后的所有空白字符
我希望這對你有用:
string testString = "Hello World ! ";
StringBuilder resultBuilder = new StringBuilder();
int charCount = 0;
foreach(char character in testString)
{
if(Char.IsWhiteSpace(character))
{
charCount++;
}
else
{
if(charCount > 0)
{
resultBuilder.Append(charCount);
charCount = 0;
}
resultBuilder.Append(character);
}
}
if (charCount > 0)
{
resultBuilder.Append(charCount);
charCount = 0;
}
testString = resultBuilder.ToString();
//your base string value
string baseString = "Hello World !";
for (int i = 0; i < baseString.Length; i++)
{
//check if have empty sumbol by position in string
if (baseString[i] == ' ')
{
//replace symbol logic
System.Text.StringBuilder sb = new System.Text.StringBuilder(baseString);
//here you can add logic for random number
sb[i] = '1';
baseString = sb.ToString();
}
}
它是空格數(' ' = 1,' ' = 3(應該是連續 3 個空格..)嗎?)或者第一個空格是 1 第二個是 2 等等?
如果它是每個空間的“索引”編號,請嘗試以下操作:
private void SpaceNumber()
{
StringBuilder sb = new StringBuilder();
int sc = 1;
string input = "bla absldf el kdslf ael dls ale xyz";
foreach (string x in input.Split(' '))
{
sb.Append(x);
sb.Append(sc);
sc++;
}
sb.Remove(sb.Length - 1, 1);
string output = sb.ToString();
}
這應該可以幫助你。 我在工作時沒有可用的開發工具,所以將其視為偽代碼:
string testString = "Hello World !";
int spaceCounter = 0;
for (int i = 0; i<testString.length; i++)
{
if (testString.charAt(i) == " ")
{
spaceCounter++;
testString.CharAt(i) = spaceCounter.toString();
}
}
不錯的答案!
輪到我了:
string theWords = "Hello World of C Sharp!";
int i = 1;
int pos = theWords.IndexOf(' ');
while (pos >= 0)
{
theWords = theWords.Substring(0, pos) + i.ToString() + theWords.Substring(pos + 1);
pos = theWords.IndexOf(' ');
i++;
}
這是一個 LINQ 解決方案:
string str = "Hello World !";
string result = str.Split()
.Zip(Enumerable.Range(1, str.Split().Length), (x, y) => x + y)
.Aggregate((x, y) => x + y).Remove(str.Length);
我找到了解決方案。 這似乎有些困難,但只要有毅力就可以了。 我建議你使用C#的StringBuilder 類(所以不要錯過添加程序集匹配)。
所以這是您可以獲得結果的腳本:
string entry = "Hello World !";
StringBuilder stringToBuild = new StringBuilder(entry);
int iForNewValue = 1;
for (int i = 0; i < entry.Length; i++)
{
Console.WriteLine("ITEM [{0}] at {1}", entry[i], i);
if (entry[i].ToString() == " ")
{
Console.WriteLine("=======> SPACE FOUNDED HERE");
stringToBuild.Replace(entry[i].ToString(), iForNewValue++.ToString(), i, 1);
}
}
Console.WriteLine("NEW STRING BUILD ========================> {0}", stringToBuild);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.