Solved

string length

Posted on 2013-12-15
4
301 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
  • 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 29

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 29

Expert Comment

by:anarki_jimbel
ID: 39724957
Not sure why :).
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Firing an ASP.NET event on selection of a JQuery datepicker 4 31
What Does This C# Code Block Do? 5 57
MailAddress in vb 4 29
Need a simple RegEx to search for two words 27 46
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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