当前位置: 动力学知识库 > 问答 > 编程问答 >

vb.net - substring and the indexOf method

问题描述:

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.

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

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

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.

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

网友答案:

You'd typically use IndexOf to -

See if a string contained something

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

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.

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.

网友答案:

Since this is a homework question, I will answer with a question or several:

  • How would you normally enter a full address into a text box?
  • How would each part of the address be distinguishable from another?
  • Once you figure out how this can be done, think about IndexOf again.
分享给朋友:
您可能感兴趣的文章:
随机阅读: