stephenlecomptejr
asked on
Convert VBA Code to C# - How to find a phrase in a string and then parse everything to the left of that value?
Please note the following code looks for a phase/value called (Phone: ) and once it's found in a string grabs all of the text to the left of it and returns the value:
I'm a lot more familiar with VBA than C# and was wondering if someone could help me with the code below that would be it's equivalent. MID function does a pretty good job of identifying text in a string if you give it a start value and LEFT I know C#'s equivalent is SubString... but if someone could convert the code to actual C# - it would be much appreciated.
I tried http://www.developerfusion.com/tools/convert/vb-to-csharp and they are still stumped and have not returned a reply. I'm not trying to be guided to articles where I can read up on how to figure it out. I'm doing other programming stuff - this is just a little nugget that I'm stuck on and would ideally like some code to try and see if it's doing the same thing in Visual C# .NET.
Thank you in advance.
I'm a lot more familiar with VBA than C# and was wondering if someone could help me with the code below that would be it's equivalent. MID function does a pretty good job of identifying text in a string if you give it a start value and LEFT I know C#'s equivalent is SubString... but if someone could convert the code to actual C# - it would be much appreciated.
I tried http://www.developerfusion.com/tools/convert/vb-to-csharp and they are still stumped and have not returned a reply. I'm not trying to be guided to articles where I can read up on how to figure it out. I'm doing other programming stuff - this is just a little nugget that I'm stuck on and would ideally like some code to try and see if it's doing the same thing in Visual C# .NET.
Thank you in advance.
Public Function NoPhoneNumber(sValue as String)
Dim newvalue As String
Dim i as Integer
For i = 1 to len(sValue)
If len(sValue) > 8 Then
If mid(sValue, i, 9) = "(Phone: )" Then
newvalue = Left(sValue, i-9)
Exit For
End if
End if
Next i
NoPhoneNumber = sValue
End Sub
Another one:
That could also be written as:
private string NoPhoneNumber(string phone)
{
return phone.Contains("(Phone: )") ? phone.Substring(0, phone.IndexOf("(Phone: )")) : "";
}
That could also be written as:
private string NoPhoneNumber(string phone)
{
int i;
return (i = phone.IndexOf("(Phone: )")) != -1 ? phone.Substring(0, i) : "";
}
ASKER
Michael74
I have for my string input: "GE Healthcare - Anesthesia Delivery (Phone: ) CAD ID: ANE0034"
And yet the result using your code comes out to : "GE Healthcare - Anesthesi"
... so Anesthesia Delivery is cut off.
I have for my string input: "GE Healthcare - Anesthesia Delivery (Phone: ) CAD ID: ANE0034"
And yet the result using your code comes out to : "GE Healthcare - Anesthesi"
... so Anesthesia Delivery is cut off.
ASKER
Mike Tomlinson, that code worked but what if I want to trouble shoot each character in the string and see it getting to the point of 9 characters as it moves from the beginning character to the end... that's what the VBA enables me to see.
With the above code, I can't see the result as it goes through the process of finding it.
I guess the benefit is that it's faster than looping through each character in the string - is that correct?
With the above code, I can't see the result as it goes through the process of finding it.
I guess the benefit is that it's faster than looping through each character in the string - is that correct?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Appreciate all the replies. Mike gets more points for taking the time to give me exactly what I wanted but both deserve 'A's. Thank you sincerely.
Open in new window