如何检查用户是否输入了字符串内的字母 [英] How to check if user inputted a letter that is inside a string

查看:67
本文介绍了如何检查用户是否输入了字符串内的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是视觉基础的新手,我想知道我是如何看到用户输入的字母是否在字符串内并且如果不是则取走分数。 

I'm new to visual basic and I was wondering how I could see if the user inputted a letter that's inside a string and take away points if it isn't. 

推荐答案

你可以使用String。包含

对于不区分大小写的搜索,创建一个代码模块并将其命名为StringExtensions并将以下代码放入其中。

For case insensitive search create a code module and name it StringExtensions and place the following code into it.

Public Module StringExtensions

    <Runtime.CompilerServices.Extension>
    Public Function Contains(ByVal sender As String, value As String, Optional comp As StringComparison = StringComparison.OrdinalIgnoreCase) As Boolean
        Return sender.IndexOf(value, comp) >= 0
    End Function
End Module

用法

Public Sub demo()
    Dim someString = "Karen Payne"
    If someString.Contains("P", StringComparison.OrdinalIgnoreCase) Then
        ' will find p or P
    End If


这篇关于如何检查用户是否输入了字符串内的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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