-1

The code I tried:

public void ConcatIntegers() {
  string s = "";

  for (int i = 0; i <= 5; i++) {
    s += i.ToString();
  }

  Console.WriteLine($ "{s}");
  Console.Read();
}   

In Above method + is used to concatenate multiple values but I was looking for anyway except join, aggregate, concatenate function, instead of + symbol I want to use string interpolation ($) directly which store concatenated string into a string variable.

string s = "";

for (int i = 0; i <= 5; i++) {
  // Some code which use string interpolation to 
  // concatenat multiple string and that result is stored in s 
  // variable.
}

Console.WriteLine($ "{s}");
Console.Read();
6
  • 1
    What language is this? I know of no language which has the keyword Public (with that casing!). Please read about how to ask good questions, as well as this question checklist. Commented Oct 2, 2018 at 6:22
  • @Tushar What is your question? Commented Oct 2, 2018 at 6:31
  • @prashant ,We use + operator to concatenate multiple strings which i shown in "The code I tried". Instead of that code I want to use string interpolation which helps concatenate multiple string and my code will be readable. Commented Oct 2, 2018 at 6:43
  • 1
    Use string builder to concat strings due to huge performance difference. Commented Oct 2, 2018 at 6:45
  • String interpolation doesn't concatenate, it formats a string with parameters. In essense it's an easier way to write String.Format. Since s is a string using interpolation here is meaningless Commented Oct 2, 2018 at 7:12

5 Answers 5

1

except join, aggregate, concatenate function, instead of + symbol I want to use string interpolation ($)

directly which store concatenated string into a string variable...

simply try:

string result = string.Empty;
for (var i = 0; i <= 5; i++) result = $"{result}{i}";
Sign up to request clarification or add additional context in comments.

Comments

1

Use StringBuilder since if you do that a lot it is much faster Use AppendFormat

StringBuilder sb = new StringBuilder();
string var1   = "abcd";
string var2   = "efgh";
sb.AppendFormat("example: {0}, {1}", var1, var2);

Comments

1

If you want to concatenate, let's try string.Concat or string.Join; with a little help of Linq (in order to get rid of for loop) we'll get

  using System.Linq;

  ...

  // static: we don't use "this" in the method
  public static void ConcatIntegers() {
    // Concatenate range of 0..5 integers: "012345"
    Console.WriteLine(string.Concat(Enumerable
      .Range(0, 6))); // 6 - we want 6 numbers: 0..5

    Console.Read();
  }

In case you want to use some format, string interpolation etc. add Select:

 public static void ConcatIntegers() {
   // "000102030405" since we apply "d2" format (each number reprsented with 2 digits)
   Console.WriteLine(string.Concat(Enumerable
     .Range(0, 6)
     .Select(i => $"{i:d2}"))); // each item in 2 digits format

   Console.Read();
 }

Comments

1

I would use StringBuilder to concatenate the string:

Your code after changes:

StringBuilder sb = new StringBuilder();
            
string s = "";
sb.Append(s);
for (int i = 0; i <= 5; i++)
{
   sb.Append(i);
}

Console.WriteLine(sb);
Console.ReadLine();

Comments

-1

I had a similar issue on using string interpolation in string.join

        string type1 = "a,b,c";
        string[] type2 = new string[3] { "a", "b", "c" };
        
        string result = string.Join(",", $"'{x}'");

In both cases, the output should be 'a','b','c'

how to use string.Join() with string interpolation for array of strings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.