Home > Back-end >  How can I work with Char Array and StringBuilder?
How can I work with Char Array and StringBuilder?

Time:06-03

I'm trying to create random character combination. Then I'd like to output it on the console. The following code does work as long as I'm using string instead of char datatypes.

The problem with using just sting datatype is, that the output will only be in upper case and I can't simply lowercase everything and only uppercase the first string.

So I tried to do it with a char array appending every character to a stringBuilder. But somehow I can't Console.WriteLine what I coded. and I don't know why... Do you guys maybe have a hint for solving the issue?

cheers

    using System;

namespace WinGen
{
    public class NamenGenerator
    {
        private int NamenLaenge { get; set; }
        private char[] BuchstabenKombination { get; set; }
        private int CharShifter { get; set; }
        private int CharPicker { get; set; }
        private int RangeMin { get; set; }
        private int RangeMax { get; set; }

        public NamenGenerator()
        {
            Random _namenLaenge = new Random();
            this.NamenLaenge = _namenLaenge.Next(4, 10);
            CharShifter = 0;
            RangeMin = 0;
            RangeMax = 25;
            BuchstabenKombination = new char[this.NamenLaenge];
        }

        public void StringGenerator()
        {
            try
            {
                while (this.NamenLaenge != 0)
                {
                    Random charakter = new Random();
                    CharPicker = charakter.Next(RangeMin, RangeMax);
                    BuchstabenKombination[CharShifter] = Convert.ToChar((Buchstaben)CharPicker);
                    this.NamenLaenge--;
                    this.CharShifter  ;
                }

                StringBuilder sb = new StringBuilder();
                string t = string.Empty;

                foreach (char c in BuchstabenKombination)
                {
                    t = Convert.ToString(c);
                    sb.Append(t);
                    Console.WriteLine(t);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
            
        }
    }
}

CodePudding user response:

So you can rewrite your loop like this:

foreach (char c in BuchstabenKombination)
{
    sb.Append(c);
}
Console.WriteLine(sb.ToString());

This will print the entire completed string to the console.

The other issue is that you're generating characters between 0 and 25, which are mostly control codes according to the ASCII chart. I understand that you tried to mitigate this by using an enum, but enums are essentially named integer values, so you're basically still generating characters from 0 and 25 on the ASCII chart.

To verify this, you can change your code to

BuchstabenKombination[CharShifter] = Convert.ToChar(CharPicker   65); // aka CharPicker   'A'

This will now generate characters A-Y.

I imagine you created an enum so that you can choose from a set of characters. Overall, I'd rewrite your code like this:

Random charakter = new Random();
StringBuilder sb = new StringBuilder();
int desiredLength = 5;
const string characters = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < desiredLength;   i)
{
    int charIdx = charakter.Next(0, characters.Length);
    sb.Append(characters[charIdx]);
}
Console.WriteLine(sb.ToString()); // outputs a 5 character long string with characters randomly chosen from the characters string

Try it online

CodePudding user response:

@DiplomacyNotWar

the current code looks like that:

    using System;
using System.Diagnostics;

namespace WinGen
{
    public class NamenGenerator
    {
        private int NamenLaenge { get; set; }
        private char[] BuchstabenKombination { get; set; }
        private int CharShifter { get; set; }
        private int CharPicker { get; set; }
        private int RangeMin { get; set; }
        private int RangeMax { get; set; }

        public NamenGenerator()
        {
            Random _namenLaenge = new Random();
            this.NamenLaenge = _namenLaenge.Next(4, 10);
            CharShifter = 0;
            RangeMin = 0;
            RangeMax = 25;
            BuchstabenKombination = new char[this.NamenLaenge];
        }

        public void StringGenerator()
        {
            try
            {
                while (this.NamenLaenge != 0)
                {
                    Random charakter = new Random();
                    CharPicker = charakter.Next(RangeMin, RangeMax);
                    //BuchstabenKombination[CharShifter] = Convert.ToChar((Buchstaben)CharPicker);
                    BuchstabenKombination[CharShifter] = Convert.ToChar(CharPicker  65);
                    this.NamenLaenge--;
                    this.CharShifter  ;
                }

                foreach (char c in BuchstabenKombination)
                {
                    char.ToLower(c);
                }

                string re = new string(BuchstabenKombination);
                char.ToLower(re[0]);
                Console.Write(re);
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
            
        }
    }
}
  • Related