简体   繁体   English

用随机值精确长度替换字符串的最佳方法 c#

[英]Best way to replace string with random values exact length c#

I'm looking to replace a string with random values - and keep the same length.我正在寻找用随机值替换字符串 - 并保持相同的长度。 However, I'd like all characters to be replaced with chars, digits to be replaced with digits.但是,我希望将所有字符替换为字符,将数字替换为数字。

I'm wondering the best way to do this.我想知道最好的方法来做到这一点。 I'm considering a for loop through each character but that could be potentially quite performance intensive.我正在考虑对每个字符进行 for 循环,但这可能会非常消耗性能。

I may be wrong, in which case please do let me know.我可能是错的,在这种情况下请告诉我。

Thanks谢谢

Unless you've got a performance requirement and/or problem, don't micro-optimize.除非您有性能要求和/或问题,否则不要进行微优化。 Just use a loop.只需使用一个循环。

You are wrong.你错了。 To know whether it is a character or a digit, you need to look at each value in the string, so you need to loop over the string in any case.要知道它是字符还是数字,您需要查看字符串中的每个值,因此无论如何都需要遍历字符串。

How else are you going to do it without looping thorough each character?如果不循环遍历每个角色,你还打算怎么做? At a minimum, you need to look to see if the character is a digit or not and replace it.至少,您需要查看字符是否为数字并替换它。 I'll assume you can make a function called RandomChar and RandomDigit.我假设你可以制作一个名为 RandomChar 和 RandomDigit 的 function。 And this will be written more c++ ish than c# ish, but you get the idea:这将比 c# ish 写得更多 c++ ish,但你明白了:

for (int i=0;i<myStr.Length();++i)
{
  c=myStr[i];
  if(isDigit(c)) 
  {
    c=RandomDigit();
  }
  else
  {
    c=RandomChar();
  }
  myStr[i]=c;
}

There's really no other way since you need to inspect each character anyway.真的没有其他办法,因为无论如何你都需要检查每个角色。

the functions isDigit, RandomDigit, and RandomChar are left as exercises to the reader.函数 isDigit、RandomDigit 和 RandomChar 留给读者作为练习。

If it is a long string it can be since changes to a string cause a new object to be created.如果它是一个长字符串,则可能是因为对字符串的更改会导致创建新的 object。 I would use a for loop but convert your string to a char array manipulate and then back to a string.我会使用 for 循环,但将您的字符串转换为 char 数组操作,然后再转换回字符串。

(I have assumed you already have methods for generating random characters.) (我假设您已经有了生成随机字符的方法。)

var source = "RUOKICU4T";
var builder = new StringBuilder(source.Length);

for (int index = 0; index < builder.Length; index += 1)
{
    if (Char.IsDigit(source[index]))
    {
        builder[index] = GetRandomDigit();
    }
    else if (Char.IsLetter(source[index]))
    {
        builder[index] = GetRandomLetter();
    }
}

string result = builder.ToString();

Consider using LINQ to help avoid explict loops.考虑使用 LINQ 来帮助避免显式循环。 You can refactor to ensure that numbers您可以重构以确保数字

static void Main()
{
    string value = "She sells 2008 sea shells by the (foozball)";

    string foo = string.Join("", value
                                .ToList()
                                .Select(x => GetRand(x))
                                );
    Console.WriteLine(foo);
    Console.Read();
}


private static string GetRand(char x)
{             
    int asc = Convert.ToInt16(x);            
    if (asc >= 48 && asc <= 57)
    {
        //get a digit
        return  (Convert.ToInt16(Path.GetRandomFileName()[0]) % 10).ToString();       
    }
    else if ((asc >= 65 && asc <= 90)
          || (asc >= 97 && asc <= 122))
    {
        //get a char
        return Path.GetRandomFileName().FirstOrDefault(n => Convert.ToInt16(n) >= 65).ToString();
    }
    else
    { return x.ToString(); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM