Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi All,

I am using c sharp

I want to add some space in my string

In vb.net i use
string1="Rose"
string1= string1+space(10)


What is the equivalent space(10) in c sharp
Posted
Updated 15-Jun-11 20:17pm
v2
Comments
[no name] 16-Jun-11 2:18am    
Edited for code block.

Hey,

you can simply do the following:

string string1 = "Rose" + new string(' ', 10);

Please remeber, strings are immutable so this can cost a lot if time in a loop.



For all other answers above me:

What are you doing there!?

Your Solutions are terrible or not working.


Best Regards
Agent J
 
Share this answer
 
Comments
CHill60 1-Apr-14 19:26pm    
A couple of years ago this might have been useful. No need to insult others though - you can always use the voting system :-)
Cyrus_Vivek 26-Jun-18 16:41pm    
https://msdn.microsoft.com/en-us/library/xsa4321w(v=vs.110).aspx
You can do the same in C# if you add a reference to Microsoft.VisualBasic (in microsoft.visualbasic.dll) see here:
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.space%28VS.80%29.aspx[^]
In case you don't want to add this reference simply use:
string1 += "          ";
 
Share this answer
 
Add space suffix to the String:

C#
string char= "Codeproject";
string padright= char.ToString().PadRight(10, ' ');



add space prefix to the string:
C#
string char= "Codeproject";
string padleft=char.ToString().PadLeft(10,' ');
 
Share this answer
 
In C# you can use
C#
string _spaceof_ten= new string(Enumerable.Range(1, 10).Select(i=>' ').ToArray())
 
Share this answer
 
v2
Use this code:

string1 = "Rose";
string1 = string1 + String.Space(10);
 
Share this answer
 
v2
Comments
Chitranjan Pd Asthana 16-Jun-11 2:31am    
string.space(10) is not available in c sharp
Hi You can simply use " ".
look at following example
C#
string mystring = "Webline";


here mystring is a string variable with initial value Webline
in the second line of code I have added a space as follows
C#
mystring = mystring + " ";


so it will add a space and actual value of variable my string will be "Webline ".
again I did concatenation string

C#
mystring = mystring + "DotNet";
            Console.WriteLine(mystring);
            Console.ReadLine();


so final output is
"Weblineindia DotNet"
you can find the space in output after Webline.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900