How to create fixed length string in c#

FUNCTION:

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);
}

USE OF FUNCTION:

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);
}

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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