Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Getting the left 50 chars of a string....

Posted on 2005-03-23
4
Medium Priority
?
276 Views
Last Modified: 2010-04-16
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...
0
Comment
Question by:kwickway
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
heintalus earned 80 total points
ID: 13619337
Just test the length of the string before you do you try to use substring on it

if (tmpstring.Length() > 50)
   newstring = tmpstring.SubString(0,50);
else
   newstring = tmpstring
0
 

Author Comment

by:kwickway
ID: 13619359
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???
0
 
LVL 1

Expert Comment

by:Mannaraprayil
ID: 13619636
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.
                             
0
 

Author Comment

by:kwickway
ID: 13619665
THanks, got it, it was a logic error on my part...
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question