?
Solved

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

Posted on 2005-03-23
4
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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