Solved

string length

Posted on 2013-12-15
4
298 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
Comment Utility
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
Comment Utility
...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
Comment Utility
@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
Comment Utility
Not sure why :).
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now