?
Solved

string length

Posted on 2013-12-15
4
Medium Priority
?
305 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

764 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