Padded String with size - CSharp System

CSharp examples for System:String Padding

Description

Padded String with size

Demo Code

//    Licensed under the Apache License, Version 2.0 (the "License");
using System.Text;
using System;/*w w w .  j a  va 2  s  . c  o m*/

public class Main{
        public static string PaddedString(String str, int size) {
         if (size == -1) {
            return str;
         }
         int dif = size - str.Length;
         if (dif > 0) {
            var buf = new StringBuilder(str);
            for (int n = 0; n < dif; ++n) {
               buf.Append(' ');
            }
            return buf.ToString();
         }
         if (dif < 0) {
            return str.Substring(0, size);
         }
         return str;
      }
}

Related Tutorials