Vaibhav Singh

Vaibhav Singh

  • NA
  • 132
  • 22.6k

Managing Spaces in String for Clean Output

Jan 9 2019 12:02 PM
Lets go into slowly.
 
Lets suppose I am building a SQL Query by actually hardcoding the syntax in code and taking parameter values for database..
The Code looks something like below 
What I want now is to show output like below which should be perfectly aligned in a single line as highlighted by Yellow Line
 
I tried storing the contents in left in string a and contents in right in string b.
Further I used string.format something like below but that didn't work out.
That actually removes the spaces
 
string alignedtext = string.Format("{0,-10} | {1,5}", a,b); 
 
So what is the best way to manage spaces such that the output looks neat and clean.
Code is below
 
  1. if (attribute_unique_index_ind == "False")  
  2. {  
  3. // table script  
  4. strb_table_script.Append("\t,[" + attribute_name + "]" + " " + attribute_datatype + attribute_precision_value + " " + attribute_not_nullable);  
  5. if (j + 1 < dv_entity_metadata.Count)  
  6. strb_table_script.Append(Environment.NewLine);  
  7. if (attribute_datatype == "VARCHAR")  
  8. changemetxt = "'Change Me'";  
  9. else if (attribute_datatype == "DATE")  
  10. changemetxt = "'01/02/3456'";  
  11. else  
  12. changemetxt = "'Change Me'";  
  13. string a = Environment.NewLine + "\t," + changemetxt + "AS" + " [" + attribute_name + "]";  
  14. string b = "/*" + attribute_not_nullable + "|" + attribute_datatype + attribute_precision_value + "< --- UPDATE */";  
  15. string alignedtext = string.Format("{0,-10} | {1,5}", a,b);  
  16. strb_sp_custom_block_script.Append(alignedtext);  
  17. }

Answers (1)