Click here to Skip to main content
14,694,039 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
How to find string length with out using string length function
Posted

Why the heck would you want to do that? And I suppose you could use a foreach statement to loop through all the chars in the string. You'd create a variable that is initially set to 0 and increments at each iteration of the loop. But that would be a huge waste of time... unless you have some really interesting reason, you should use the Length property.
   
More homework, I suspect. Start by trying to analyze the problem and figure out what you need to do in real world terms to get the result, then turn that into code. For example how would you calculate the length of this sentence; oh hold on there's some more here ...
   
Why? Anyway, if it's not cheating you can convert it to a character array and return the length of that instead
int length = "Test string".ToCharArray().Length;
   
I would count the characters...
i.e. dumb question => dumb answer.
:)
   
That's a fairly strange request. If you want to do it the C way, then iterate through every character until you hit \0. You could also use aspdotnetdev's way. But chances are that the .Net framework can do things far more efficiently.
   
Use the all-powerful LiquidNitrogen object.
   
I see from your profile that you're working as a developer, so I assume this is not homework. It's attracted a lot of joke answers because it seems like a dumb question - the string class has a length property, so why not use it ? In my experience, people who ask questions like this, have a reason that is not immediately clear for asking them. You should edit your post to explain what it is you're trying to do, because usually, when people do that, the solution is not what they expected it to be, and we can only help with that, if you explain your motives.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900