我有一些话:
table
computer
mouse
...
而我的目标是保留前三个字母,其余的替换为字母x,例如:
tabxx
comxxxxx
mouxx
我正在使用c#。
有人可以帮忙吗?
我会写一个新方法。显然,您有使用此功能的原因,因此,请确保该名称代表该进程,即ShowPassword。如@jdweng所述,请使用substring函数。
ReplaceStrFunction方法
它有1个输入,您需要将其传递给字符串数组。然后它将返回所有更新的字符串条目的数组。
public static string[] ReplaceStrFunction(string[] strArray)
{
//Initialise Count
var count = 0;
//Make new Array to store the amended strings. Use the passed in array to dynamically determine the length.
string[] replacedStrItem = new string[strArray.Length];
//Iterate over each item in the string array
foreach (string strItem in strArray)
{
//Replace each substring afer 3 charcters with an 'X'
replacedStrItem[count] = strItem.Substring(0, 3) + new string('x', strItem.Length - 3);
//Increment count by 1 each iteration
count++;
}
//Return string array full of amended items
return replacedStrItem;
}
调用函数:
//Build String array
string[] strArray = new string[]{"table","computer","mouse" };
ReplaceStrFunction(strArray);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句