如何在VB.NET中获取字符串中出现的字符数 [英] How do I get the number of occurrences of a character in a string in VB.NET

查看:267
本文介绍了如何在VB.NET中获取字符串中出现的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就这样,你们都知道,我在网上搜索所有发布的问题和其他人。我的问题是我在VB 2012中创建了一个字符串函数(有一些参数)。它以25 x 25字符数组搜索特定位置,并返回长度为3到10个字符的字符串。这个字符串将由大写字母(单词)和*组成。



我想使用:



Just so that YOU all know, I DID search through all of the posted questions and others online. My issue is that I have created a string function in VB 2012 that (has some arguments). It searches specific locations in a 25 x 25 character array and returns a string of 3 to 10 characters in length. This string will be made up of capital letters (of words) and "*".

I'd like to use:

String.Replace("*", "").Count





确定返回字符串中这些字符的数量。我该怎么做?



我尝试过:



我试过了:





to determine the number of these characters in the returned string. How do I do this?

What I have tried:

I've tried:

MyFunction([some arguments]).Replace("*", "").Count





但它不起作用。我甚至认为我的函数没有正确返回字符串。



but it dont work. I don't even think my function is returning the string properly.

推荐答案

看看这个:计算字符串中的行数 [ ^ ] - 它是关于行的,但这只是意味着它专门寻找'\ n' - 改变它很简单 - 代码是C#而不是VB,但它主要是非常简单的代码。如果您无法理解,那么这样的在线转换器可能有所帮助:代码转换器C#到VB和VB到C# - Telerik [< a href =http://converter.telerik.com/target =_ blanktitle =New Window> ^ ]

它提供了不同的方法它和它们的相对速度一起。
Have a look at this: Counting Lines in a String[^] - it's about lines, but that just means that it specifically looks for '\n' - it's simple to change that - and the code is C# rather than VB, but it's mostly pretty trivial code. If you can't understand it, then online converters like this may help: Code Converter C# to VB and VB to C# – Telerik[^]
It presents quite a few different ways to do it, together with their relative speeds.


引用:

但它不起作用。我甚至认为我的函数没有正确返回字符串。

but it dont work. I don't even think my function is returning the string properly.



显示你得到的结果可以帮助你知道函数是否正常工作。


Displaying the result you get may help you to know if the function works properly or not.

MyFunction([some arguments]).Replace("*", "").Count



替换是一个RegEx函数,在RegEx中,'*'是一个特殊的字符。你可能需要改变它吗'\ *'



只是一些有趣的链接来帮助建立和调试RegEx。

这里是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx的工具的链接并调试它们:

.NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]

Expresso正则表达式工具 [ ^ ]

RegExr:Learn,Build,&测试RegEx [ ^ ]

在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]

这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]

这个网站也在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容:

Regexper [ ^ ]


Replace is a RegEx function, and in RegEx , '*' is a special char. you probaly need to change it ti '\*'

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]


这篇关于如何在VB.NET中获取字符串中出现的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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