Aaron
Aaron

Reputation: 161

How can I insert a blank space (" "), when I concatenate a string?

This question is for C Sharp (and Java maybe :).

When I want to display a message to the console, I want to insert after each "+" a blank space. How can I do this, without inserting manually that blank space?

Upvotes: 16

Views: 52361

Answers (8)

Nishan
Nishan

Reputation: 4441

In C# you can use String Interpolation as well using the $ special character, which identifies a string literal as an interpolated string

string text1 = "Hello";
string text2 = "World!";
Console.WriteLine($"{text1}, {text2}");

Output

Hello, World!

From Docs

String interpolation provides a more readable and convenient syntax to create formatted strings than a string composite formatting feature.

// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);

// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");

Both calls produce the same output that is similar to:

Hello, Mark! Today is Wednesday, it's 19:40 now.

Upvotes: 0

Ergwun
Ergwun

Reputation: 12978

In C#:

string.Join(" ", "Foo", "Bar", "Baz");

In Java:

String.join(" ", "Foo", "Bar", "Baz");

Each of these methods permits a variable number of strings to join, and each has various overloads to pass in collections of strings too.

Upvotes: 6

Dean Chalk
Dean Chalk

Reputation: 20471

try this

var text = string.Join(" ", new[] {foo, bar, other });

Upvotes: 18

Ivan Stepuk
Ivan Stepuk

Reputation: 138

Do you mean a concatenation of strings or just a '+' character? In Java, if there are lot of parameters to show within an output string you can use String.format method like this: String.format("First: %s, second: %s, third: %s etc", param1, param2, param3). In my opinion it's more readable than chained concatenation with '+' operator.

Upvotes: 0

anirvan
anirvan

Reputation: 4877

Or in Java you can use the printf variant of System.out:

System.out.printf("%s %s", foo, bar);

Remember to put in a "\n" [line feed] at the end, if there are multiple lines to print.

Upvotes: -1

sharepointmonkey
sharepointmonkey

Reputation: 667

if you're looking for a way to tidy your printing routine try String.Format e.g.

Console.WriteLine(String.Format("{0} {1}", string1, string2));

Upvotes: 6

Erkan Haspulat
Erkan Haspulat

Reputation: 12572

You can replace "+" with "+ ". Something like this:

new String("Foo+Bar").replace("+", "+ ");

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1503290

You can't, really - just put it in explicitly:

Console.WriteLine(foo + " " + bar);

or

System.out.println(foo + " " + bar);

I mean you could write a method with a parameter array / varargs parameter, e.g. (C#)

public void WriteToConsole(params object[] values)
{
    string separator = "";
    foreach (object value in values)
    {
        Console.Write(separator);
        separator = " ";
        Console.Write(value);
    }
}

... but personally I wouldn't.

Upvotes: 7

Related Questions