使用vb.net进行字符串操作 [英] String Manipulation using vb.net

查看:338
本文介绍了使用vb.net进行字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找特定字母,在给定的字符串中出现了多少次,请给我解决方案

How to find particular letter,how many times occur in the given string pl give me solution

推荐答案

您可以使用LINQ方法:

You can do it the LINQ way:

string s = "How are you doing?";
int count = (from char c in s.ToCharArray()
             where c.Equals('o')
             select c).Count();



这将返回3.

更新:VB版本(感谢Simon):



This will return 3.

Update : The VB version(thanks to Simon):

Dim s As String = "How are you doing?"
Dim count As Integer = (From c As Char In s.ToCharArray() _
                        Where c.Equals("o"c) _
                        Select c).Count()


我知道的2种方法

1.带有子字符串的循环方法

2 methods that I know

1. Loop method with Substring

Dim s As String = "How are you doing?"
Dim Counter As Integer = 0
Dim CharToCount As Char = "o"

For i As Integer = 0 To s.Length - 1
  If s.Substring(i, 1) = CharToCount Then Counter += 1
Next

MsgBox(Counter)



2. LinQ感谢 Tarun K.S



2. LinQ thanks to Tarun K.S

Dim s As String = "How are you doing?"
Dim Count As Integer = (From c As Char In s.ToCharArray Where c.Equals("o"c) Select c).Count


从逻辑上讲,您应该循环计数.

但最简单的是,您可以使用String.Split
Logically you should loop and count.

But easiest is, you can use String.Split
YourText.Split("c").Length - 1



它的作用是使用字符分割文本.



What it does is, split your text using character.

Text = abcdcbac <br />
Split = ab,d,ba,<blank>


您可以减去1以获得计数. ;)


You can subtract 1 to get the count. ;)


这篇关于使用vb.net进行字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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