kwickway
asked on
Getting the left 50 chars of a string....
Hello, simple simple question but can't find it or figure it out, im trying to get the 50 left most chars of a string, however sometimes the string may be smaller than 50 chars, or may be a lot bigger, im using the substring metthod but it errors out when the string is too small.
Thanks...
Thanks...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What is the result you are getting now?
Well You want the first 75 characters of the txtObject.Text , right?
Now you build the new txtObject.Text by applying replace on txtText before checking the length. Because the new txtObject length can be more that 75. So in the else part you will end with the new txtObject which is having length greater than 75. So try this:
this.txtValue.Text=txtObje ct.Text.Le ngth.ToStr ing();
txtObject.Text = txtObject.Text + txtText.Text.Replace(" ", "").Replace("-", "").Replace(".", "").Replace(":", "").Replace(",", "").Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "");
if (txtObject.Text.Length > 75)
{
txtObject.Text = txtObject.Text.Substring(0 ,75);
}
You dont even need the else part.
Hope this will help.
Well You want the first 75 characters of the txtObject.Text , right?
Now you build the new txtObject.Text by applying replace on txtText before checking the length. Because the new txtObject length can be more that 75. So in the else part you will end with the new txtObject which is having length greater than 75. So try this:
this.txtValue.Text=txtObje
txtObject.Text = txtObject.Text + txtText.Text.Replace(" ", "").Replace("-", "").Replace(".", "").Replace(":", "").Replace(",", "").Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "");
if (txtObject.Text.Length > 75)
{
txtObject.Text = txtObject.Text.Substring(0
}
You dont even need the else part.
Hope this will help.
ASKER
THanks, got it, it was a logic error on my part...
ASKER
if (txtObject.Text.Length > 75)
{
txtObject.Text = txtObject.Text + txtText.Text.Replace(" ", "").Replace("-", "").Replace(".", "").Replace(":", "").Replace(",", "").Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "");
txtObject.Text = txtObject.Text.Substring(0
}
else
{
txtObject.Text = txtObject.Text + txtText.Text.Replace(" ", "").Replace("-", "").Replace(".", "").Replace(":", "").Replace(",", "").Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "");
}
this is not working, can you help???