question about strings & stringbuilder!

this is the code
namespace Palindrome_Finder
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to Our Palindrome Finder");
Console.WriteLine("Enter a Word or a Sentence to find if it's a Palindrome");
string UserInput = Console.ReadLine();
string processedInput=cleaner(UserInput);
Console.WriteLine($"Reversed Input: {processedInput}");
Console.ReadLine();

}
static string cleaner(string input)
{
string cleanedInput = input;
Console.WriteLine($" Original Input:{cleanedInput}");
foreach (char c in cleanedInput)
{

if(char.IsLetterOrDigit(c))
{
cleanedInput.Append(c);
}

}
string reversedInput = new string(cleanedInput.Reverse().ToArray());
return reversedInput;

}

}
}
namespace Palindrome_Finder
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to Our Palindrome Finder");
Console.WriteLine("Enter a Word or a Sentence to find if it's a Palindrome");
string UserInput = Console.ReadLine();
string processedInput=cleaner(UserInput);
Console.WriteLine($"Reversed Input: {processedInput}");
Console.ReadLine();

}
static string cleaner(string input)
{
string cleanedInput = input;
Console.WriteLine($" Original Input:{cleanedInput}");
foreach (char c in cleanedInput)
{

if(char.IsLetterOrDigit(c))
{
cleanedInput.Append(c);
}

}
string reversedInput = new string(cleanedInput.Reverse().ToArray());
return reversedInput;

}

}
}
people say me use stringbuilder instead of appending to a string they say the way I did can be slower and resource consuming he don't reply further thought he is busy man can any gentleman teach me why is it he just said strings are immutable which is also what I don't think cuz I can add new value to strings I can assign new value to strings why they say they are immutable! help me this is confusing for me!
4 Replies
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
immutable means "a" + "b" will create a new string "ab" and the previous 2 wont change you cant change strings, only create a new one
Angius
Angius6mo ago
// we have "0" in memory
var s = "0";
// now we have "0", "abc", and "0abc"
s = s + "abc";
// "0", "abc", "0abc", "de", "0abcde"
s = s + "de";
// and so on
// we have "0" in memory
var s = "0";
// now we have "0", "abc", and "0abc"
s = s + "abc";
// "0", "abc", "0abc", "de", "0abcde"
s = s + "de";
// and so on
var sb = new StringBuilder();
// we have "0" in the stringbuilder memory
sb.Append("0");
// now "0", "abc"
sb.Append("abc");
// now "0", "abc", "de"
sb.Append("de");
// now "0", "abc", "de" in the stringbuilder
// and a "0abcde" string
var s = sb.ToString();
var sb = new StringBuilder();
// we have "0" in the stringbuilder memory
sb.Append("0");
// now "0", "abc"
sb.Append("abc");
// now "0", "abc", "de"
sb.Append("de");
// now "0", "abc", "de" in the stringbuilder
// and a "0abcde" string
var s = sb.ToString();
Appending to string stores every iteration in memory Stringbuilder stores the individual elements, and only glues them together when requested It's not that different from doing
var sb = new List<string>();
sb.Add("0");
sb.Add("abc");
sb.Add("de");
var s = string.Join("", sb);
var sb = new List<string>();
sb.Add("0");
sb.Add("abc");
sb.Add("de");
var s = string.Join("", sb);
But stringbuilder has plenty of additional functionality
Muhammad Hammad
Muhammad HammadOP6mo ago
thanks for this information! zzzzzzzzzzzzzzzz and eeeeeeeeeeeeeeeee C# is cool
sibber
sibber6mo ago
lmao
Want results from more Discord servers?
Add your server