Link to home
Start Free TrialLog in
Avatar of kwickway
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...
ASKER CERTIFIED SOLUTION
Avatar of heintalus
heintalus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kwickway
kwickway

ASKER

this.txtValue.Text=txtObject.Text.Length.ToString();
                        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,75);
                        }
                        else
                        {
                            txtObject.Text = txtObject.Text + txtText.Text.Replace(" ", "").Replace("-", "").Replace(".", "").Replace(":", "").Replace(",", "").Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "");
                        }


this is not working, can you help???
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=txtObject.Text.Length.ToString();
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.
                             
THanks, got it, it was a logic error on my part...