Solved

string length

Posted on 2013-12-15
4
300 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 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