如何在不使用字符串长度函数的情况下查找字符串长度 [英] How to find string length with out using string length function

查看:118
本文介绍了如何在不使用字符串长度函数的情况下查找字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用字符串长度函数查找字符串长度

How to find string length with out using string length function

推荐答案

为什么要这么做?而且我想您可以使用foreach语句遍历字符串中的所有字符.您将创建一个变量,该变量最初设置为0,并在每次循环迭代时递增.但这将浪费大量时间...除非您有一些非常有趣的原因,否则应使用Length属性.
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;


这篇关于如何在不使用字符串长度函数的情况下查找字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆