2 Replies - 239 Views - Last Post: 31 October 2011 - 10:01 AM Rate Topic: -----

#1 CyberSlayer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-October 11

How can I shorten/trim my output?

Posted 31 October 2011 - 09:08 AM

I'm creating a ToString() method and I want to output 6 variables to a console. These variables are taken from a Data Reader. I want everything to fit on one line (they aren't currently). Is there a way I can limit the number of characters that are outputted or even limit the number of characters that get stored into the variable?
Is This A Good Question/Topic? 0
  • +

Replies To: How can I shorten/trim my output?

#2 ZevhEr  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 38
  • Joined: 18-July 09

Re: How can I shorten/trim my output?

Posted 31 October 2011 - 09:22 AM

Yes you Can.

Let say that You get DataReader value and you store it to string variable like below


        public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string testVar;

        private void Form1_Load(object sender, EventArgs e)
        {
            testVar = "My Project";
            // 'My Project' is char read from DataReader
        }

        //If you Want to Limit the Output, Create function to substring its string
        private string _SetLimit(string val, int limit)
        {
            return val.Substring(0, limit);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Console.WriteLine(_SetLimit(testVar, 2));
            //Print out 'My'
        }
    }




If you want to limit string that store
        private void Form1_Load(object sender, EventArgs e)
        {
            testVar = _SetLimit("My Project", 2);
            // Store 'My' into Variable
        }


Was This Post Helpful? 1
  • +
  • -

#3 CyberSlayer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-October 11

Re: How can I shorten/trim my output?

Posted 31 October 2011 - 10:01 AM

Sorry, what I ended up doing was padding the string variables a certain amount and then taking the substring that would start at index 0 and go until that amount. The padding keeps me from getting an Out of Bounds Exception or whatever it's called. Thanks for the help though :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1