In C#, you can insert variable values in the middle of a string using string interpolation, which allows you to embed expressions and variables inside a string literal by enclosing them in curly braces {}. Here's an example:
string name = "John"; int age = 30; double height = 1.75; string message = $"My name is {name}, I'm {age} years old, and my height is {height:F2} meters."; In this example, the string interpolation feature is used to create a new string message that includes the values of the name, age, and height variables in the middle of the string. The syntax for string interpolation is to prefix the string with a $ character and then enclose expressions and variables inside curly braces {}. You can also include format specifiers inside the curly braces to format the variable value, such as :F2 to display a double value with two decimal places.
Alternatively, you can also use the String.Format() method to insert variable values into a string. Here's an example:
string name = "John"; int age = 30; double height = 1.75; string message = String.Format("My name is {0}, I'm {1} years old, and my height is {2:F2} meters.", name, age, height); In this example, the String.Format() method is used to create a new string message that includes the values of the name, age, and height variables in the middle of the string. The syntax for using String.Format() is to specify placeholders {0}, {1}, etc. in the string, and then pass the variable values as arguments to the method in the order of the placeholders. You can also include format specifiers inside the placeholders, such as :F2 to display a double value with two decimal places.
C# insert variable into string at specific position
string originalString = "The temperature is {0} degrees Celsius."; int temperature = 25; string formattedString = string.Format(originalString, temperature); Console.WriteLine(formattedString); // Output: The temperature is 25 degrees Celsius. C# insert multiple variables into string
string originalString = "The price of {0} is {1} dollars."; string item = "apple"; decimal price = 1.50m; string formattedString = string.Format(originalString, item, price); Console.WriteLine(formattedString); // Output: The price of apple is 1.50 dollars. C# interpolate variable into string
int age = 30; string message = $"I am {age} years old."; Console.WriteLine(message); // Output: I am 30 years old. C# concatenate variables into string
string name = "John"; int age = 25; string message = "My name is " + name + " and I am " + age + " years old."; Console.WriteLine(message); // Output: My name is John and I am 25 years old.
C# insert variable value between strings
string greeting = "Hello"; string name = "Alice"; string message = $"{greeting}, {name}!"; Console.WriteLine(message); // Output: Hello, Alice! C# insert variable value into string with placeholders
string template = "My name is {0} and I am {1} years old."; string name = "Emma"; int age = 35; string formattedString = string.Format(template, name, age); Console.WriteLine(formattedString); // Output: My name is Emma and I am 35 years old. C# insert variable value into string using StringBuilder
StringBuilder to insert variable values into a string.StringBuilder stringBuilder = new StringBuilder("Today is "); string dayOfWeek = "Monday"; stringBuilder.Append(dayOfWeek); stringBuilder.Append("!"); string message = stringBuilder.ToString(); Console.WriteLine(message); // Output: Today is Monday! C# insert variable value into formatted string
int quantity = 3; string item = "books"; string message = $"I have {quantity} {item}."; Console.WriteLine(message); // Output: I have 3 books. C# insert variable value into string array
string[] stringArray = { "apple", "banana", "cherry" }; string valueToInsert = "grape"; int index = 1; // Insert "grape" at index 1 stringArray[index] = valueToInsert; C# insert variable value into string list
List<string> stringList = new List<string> { "red", "blue", "green" }; string valueToInsert = "yellow"; int index = 2; // Insert "yellow" at index 2 stringList.Insert(index, valueToInsert); revert openxml robotframework-ide twitter-bootstrap-3 delegates react-native-navigation-v2 laravel-jobs touchablehighlight entity-framework-core-2.1 cross-join