Solved

Convert VBA Code to C# - How to find a phrase in a string and then parse everything to the left of that value?

Posted on 2015-02-02
7
344 Views
Last Modified: 2015-02-03
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.

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

Open in new window

0
Comment
Question by:stephenlecomptejr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40585287
Here is code which should achieve what you are looking for

Public string NoPhoneNumber(string input)
{
   If (input.IndexOf("(Phone: )") == -1)
   {
      return "";
   }
   
   return input.Substring(0, input.Length - input.IndexOf("(Phone: )"));
}

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40585324
Another one:
        private string NoPhoneNumber(string phone)
        {
            return phone.Contains("(Phone: )") ? phone.Substring(0, phone.IndexOf("(Phone: )")) : "";
        }

Open in new window


That could also be written as:
        private string NoPhoneNumber(string phone)
        {
            int i;
            return (i = phone.IndexOf("(Phone: )")) != -1 ? phone.Substring(0, i) : "";
        }

Open in new window

0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40585365
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.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40585369
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?
0
 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 150 total points
ID: 40585380
Sorry about that. Use

Public string NoPhoneNumber(string input)
{
   If (input.IndexOf("(Phone: )") == -1)
   {
      return "";
   }
   
   return input.Substring(0, input.IndexOf("(Phone: )")-1).Trim()
}

Open in new window


Note: I added trim to strip the whitespace. If this was wrong just remove the Trim() function from the call
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 350 total points
ID: 40585564
"I guess the benefit is that it's faster than looping through each character in the string - is that correct?"

I'm not sure what the underlying code for IndexOf actually does.  It might be looping for you.

If you really want to loop, then:
        private string NoPhoneNumber(string phone)
        {
            string lookFor = "(Phone: )";
            if (phone.Length > lookFor.Length)
            {
                for(int i = 1; i <= (phone.Length - lookFor.Length); i++)
                {
                    if (phone.Substring(i, lookFor.Length).Equals(lookFor))
                    {
                        return phone.Substring(0, i);
                    }
                }
            }
            return "";
        }

Open in new window

0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 40586076
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.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question