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?
How can I shorten/trim my output?
Page 1 of 12 Replies - 139 Views - Last Post: 31 October 2011 - 10:01 AM
Replies To: How can I shorten/trim my output?
#2
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
If you want to limit string that store
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
}
#3
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|