Solved

string length

Posted on 2013-12-15
4
303 Views
Last Modified: 2013-12-17
i have a string whose length might be less or greater than 30 characters. I want to check if the length is more than 30 chars then
replace or trim the string to length 30 and if less then leave it like it is. In c#.net
0
Comment
Question by:Moizsaif123
[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
  • 2
4 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 39720858
String s = "1234567890123456789012345678901234567890";
            if (s.Length > 30)
                s = s.Remove(30);
//s is now 30 chars long - actually the if statement isn't actually required in this case
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39722195
...actually the if statement isn't actually required in this case

Why not? In the .NET Framework, strings are zero-based. The value of the startIndex parameter can range from zero to one less than the length of the string instance.
If startIndex specifies a position that is not within this string the ArgumentOutOfRangeException is thrown.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39723252
@anarki_jimbel

Good point, for some reason it didn't throw an exception when I tested it here without the if statement.  I've moved it into a button click event and now it does.  Hmmm?   (I didn't code it with an exception handler.)
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39724957
Not sure why :).
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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

690 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