How to create fixed length string in c#


String GetFixedLengthString(string input, int length)
input = input ?? string.Empty;
input = input.Length > length ? input.Substring(0, length) : input;
return string.Format("{0,-" + length + "}", input);


protected void search_Click(object sender, EventArgs e)
String Qs = "SELECT CourseRegistrationEnd FROM T_Term_Setup WHERE TermId='" + ddlTermName.Text.Trim() + "'";
DataSet ds = new DataSet();
DataTable dtable = new DataTable();
ds = ClsCommon.GetAdhocResult(Qs);
dtable = ds.Tables[0];
RegistrationEndDate.Text = GetFixedLengthString(dtable.Rows[0]["CourseRegistrationEnd"].ToString(), 10);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s