R
rhkodiak
I was in an interview question what is the most efficient way to sort
a string for example "010101", the result would then be "000111", and
write the code on how to do this. I wrote the code for the
interviewers and I was told I did ok, but a stringbuilder would have
been better to use. In this storing the string in an array and
calling the sort method is not a solution for this problem. If anyone
can help explain how using a stringbuilder is better in this way
please let me know and please show the code on how to use the
stringbuilder because I cannot figure it out. I have provided sample
code for what I did. Thanks!
public string GetString(string text)
{
string t1 = string.Empty, t2 = string.Empty;
for(int i = 0; i < text.Length; i++)
{
if(text.ToString() == "0")
{
t2 += text.ToString();
}
else
{
t2 += text.ToString();
}
}
return t1+t2;
}
a string for example "010101", the result would then be "000111", and
write the code on how to do this. I wrote the code for the
interviewers and I was told I did ok, but a stringbuilder would have
been better to use. In this storing the string in an array and
calling the sort method is not a solution for this problem. If anyone
can help explain how using a stringbuilder is better in this way
please let me know and please show the code on how to use the
stringbuilder because I cannot figure it out. I have provided sample
code for what I did. Thanks!
public string GetString(string text)
{
string t1 = string.Empty, t2 = string.Empty;
for(int i = 0; i < text.Length; i++)
{
if(text.ToString() == "0")
{
t2 += text.ToString();
}
else
{
t2 += text.ToString();
}
}
return t1+t2;
}