yithril
yithril

Reputation: 67

Replace Method not working?

I have a set of strings that all have the same pattern: "Jones, Bob - NO TIME" where it is the last name, comma, first name, then a hyphen and no time. I want to strip the last name and first name and save them to variables. I tried:

dim emp_name as string = "Jones, Bob - NO TIME"
emp_name = Replace(emp_name, "- NO TIME", "")
dim _Name as string() = emp_name.Split(",")
Dim first_Name as string = Name(0)
Dim last_Name as string = Name(1)

For some reason replace doesn't get rid of the "- No Time" and I'm not sure why.

Upvotes: 0

Views: 241

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460340

It is _Name not Name, i guess Name is a different property of your class.

Why you don't use the .NET method String.Replace but the old VB function Replace?

But since you want to remove the token at the end, you dont want to replace every occurence:

Dim empName as string = "Jones, Bob - NO TIME"
Dim index = empName.LastIndexOf("- NO TIME")
empName = If(index = -1, empName, empName.Remove(index))
Dim tokens = empName.Split(","c)
Dim lastName as string = tokens(0).Trim()
Dim firstName as string = tokens.Last().Trim()

Upvotes: 1

Youssef13
Youssef13

Reputation: 4986

Replace this two lines:

Dim first_Name as string = Name(0)
Dim last_Name as string = Name(1)

with

Dim first_Name as string = _Name(0)
Dim last_Name as string = _Name(1)

Because your array is _Name not Name

Upvotes: 0

Related Questions