Click here to Skip to main content
15,892,059 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.
 
Share this answer
 
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 ...
 
Share this answer
 
Why? Anyway, if it's not cheating you can convert it to a character array and return the length of that instead
C#
int length = "Test string".ToCharArray().Length;
 
Share this answer
 
I would count the characters...
i.e. dumb question => dumb answer.
:)
 
Share this 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.
 
Share this answer
 
Use the all-powerful LiquidNitrogen object.
 
Share this answer
 
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.
 
Share this answer
 

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