COCO COCO - 6 months ago 12
Vb.net Question

Multiple string delimiters

I'm a total beginner in the VB language and I have a string in the format:


Smith, Alex (asmith@gmail.com)


I'm attempting to isolate "asmith" from this string. I'm assuming that we would have to split at the "(" and then at the "@" and obtain the 1st index?

Any help is appreciated.

Answer

I'm assuming that we would have to split at the "(" and then at the "@" and obtain the 1st index?

Your approach is correct. A literal translation of your algorithm to VB.NET would be:

Dim firstPartOfMailAddress = text.Split("("c)(1).Split("@"c)(0)

Explanation:

  • "("c is a character literal. Split expects a character, not a string (that would be "(").
  • Arrays are zero-based in VB.NET and indexed using the (...) operator. Thus, you take the second part (1) of the first split and the first part (0) of the second split.

Notes:

  • This code does not include any error handling and will fail horribly if the text does not contain the character (.
  • An alternative would be to extract the value using a regular expression, but if your are new to the language, splitting is a much easier option.