开发者

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

开发者 https://www.devze.com 2023-03-08 19:31 出处:网络
开发者_运维技巧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 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.

I may be wrong, in which case please do let me know.

Thanks


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.


Unless you've got a performance requirement and/or problem, don't micro-optimize. Just use a loop.


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. And this will be written more c++ ish than c# ish, but you get the idea:

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.


If it is a long string it can be since changes to a string cause a new object to be created. I would use a for loop but convert your string to a char array manipulate and then back to a string.


(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. 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(); }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号