substring 和 indexOf 方法 [英] substring and the indexOf method

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

问题描述

我在 Visual Basic 2010 中的任务是构建一个包含两个文本框的订单表单,一个用于名称,另一个用于地址.

My assignment in Visual Basic 2010 is to build a order form that has two text boxes, one for the name and the other for the address.

我们假设在地址上使用 IndexOf 方法.

And we're suppose to use the IndexOf method on the address.

我知道 IndexOf 返回字符的位置和字符数.

I understand that IndexOf returns the position of a character and the number of characters.

在此实例中对地址使用 IndexOf 方法的目的是什么?

What would be the purpose of using the IndexOf method on the address in this instance?

我不明白当用户输入地址是数字和字符串时我会搜索什么.

I don't understand what I would be searching for when the user types in it's address that's going to be numbers and string characters.

我想我有点理解 IndexOf 方法的作用,但为什么以及我会用它来实现什么?

I think I understand what the IndexOf method does, somewhat, but why and what would I use it to achieve?

推荐答案

您通常会使用 IndexOf 来 -

You'd typically use IndexOf to -

查看字符串是否包含某些内容

See if a string contained something

If someString.IndexOf("Avenue") > - 1 Then
   'do something
End If

获取字符串中某个值的起始位置,然后可用于提取字符串的一部分.例如someString.Substring(someString.IndexOf("@"),10) 将从字符串中找到@"字符的位置开始获取接下来的十个字符.

Get the start position of a value in a string , this could then be used to extract part of the string. e.g. someString.Substring(someString.IndexOf("@"),10) would get then next ten characters starting from where the "@" character was found in your string.

请记住,如果 IndexOf 找不到您要搜索的字符串,它将始终返回 -1,因此您的代码必须处理这种情况.

Bear in mind you'll always need to handle scenarios where IndexOf will return -1 if it does not find the string your searching for, so your code will have to handle that eventuality.

这篇关于substring 和 indexOf 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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