计算一个角色 [英] Counting A Character

查看:55
本文介绍了计算一个角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有一个文本框和textBox1.Text例如:杰克#Alex #Marlo #Jimmy #Bob



现在我想知道文本框中有多少[],我知道它的4 []在那句话中,但不知道如何计算

plzzzzzz帮助我

hi guys i have a textbox and textBox1.Text is for example: Jack#Alex#Marlo#Jimmy#Bob

now i wanna find out how many [#] is there in the textbox, i know its 4 [#] in that sentence but dont know how to get that count
plzzzzzz help me

推荐答案

受到关于不必要的讨论的启发ToArray()方法调用,我决定发布这个答案,以提供有关两种解决方案中使用的方法之间差异的更多信息(解决方案1和解决方案2)。



最重要的陈述是: 两种解决方案都是等价的 并返回完全相同的结果!

即使差异很小(在代码编译和执行的范围 - 见下表),在这种情况下无关紧要!只有在执行字符串比较时才会有所不同。



解决方案1 ​​ 解决方案2
查询语法 方法语法
IL指令(由LinqPad 4生成) ):

Inspired by the discussion about "unnecessary ToArray() method call", i decide to post this answer to bring more information about the "differences" between methods used in a both solutions (solution 1 and solution 2).

The most important statement is: Both solutions are equivalent and return exactly the same result!
Even if there are few differences (in a scope of code compilation and execution - see below table), it doesn't matter in this case! It might makes a difference only in case when the set of strings comparison is performed.

Solution 1 Solution 2
query syntax method syntax
IL instructions (generated by LinqPad 4):
IL_0001:  ldstr       "Jack#Alex#Marlo#Jimmy#Bob"
IL_0006:  stloc.0     // s
IL_0007:  ldloc.0     // s
IL_0008:  call        System.Linq.Enumerable.ToArray
IL_000D:  ldsfld      UserQuery.CS


<> 9__CachedAnonymousMethodDelegate1
IL_0012:brtrue.s IL_0027
IL_0014:ldnull
IL_0015:ldft n b__0
IL_001B:newobj System.Func< System。 Char ,System.Boolean> .. ctor
IL_0020:stsfld UserQuery.CS
<>9__CachedAnonymousMethodDelegate1 IL_0012: brtrue.s IL_0027 IL_0014: ldnull IL_0015: ldftn b__0 IL_001B: newobj System.Func<System.Char,System.Boolean>..ctor IL_0020: stsfld UserQuery.CS


<> 9__CachedAnonymousMethodDelegate1
IL_0025:br.s IL_0027
IL_0027:ldsfld UserQuery.CS
<>9__CachedAnonymousMethodDelegate1 IL_0025: br.s IL_0027 IL_0027: ldsfld UserQuery.CS


这篇关于计算一个角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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