×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / a small VB question
    I have a phone number like (416)123-45-67*007,
    I want to reaplce the second hyphen with a NULL string("")
    (because it's a obvious mistake in a telephone number),
    what would you do to solve the problem?
    (hint: you can not use the real number like 45,67 as
    your search symbol, because I have a lot of different
    numbers like this, I need a general solution)

    dim sVariable as string
    sVariable = "(416)123-45-67*007"

    then......?
    • use substring() to find the second hyphen, then remove it. so (416)123-45-67 will become to (416)123-4567, that's what you want, right?
      • em.....,. it's not I'm looking for
        the actulal situation is:

        I got phone number looks like 416-123-45-67-789,
        I want to remove the second and third hyphens
        and replace the last one with "ext".
        sometimes the string comes up as 416-123-4567-789
        which is almost correct, just needs to replace last
        one with "ext" and replace the second "-" with "",
        any general solutions?

        and what about situations 416-12345-67-789?

        I remember I saw very short piece of code to address this.
        can not remember now.....
        • a dumb way
          remove all hyphens
          check string length
          if length < 10
          error
          else if length=10
          make it 416-123-4567
          else if length >10
          make it 416-123-4567 ext 8999
          • too dumb to...........
        • try
          remove all hyphens and non-numeric characters.
          then add two hyphens after the thrid number and sixth number.
          add "Ext " after 12th(10 digital and 2 hyphens) character.
          I think it should work for 10 digital phone numbers.
          • and let this function run on client, fired when client try to write DB
          • a smart idea, but.....
            we have to deal with variable length digit between first dash and last one,
            here is an extreme example(normally is between 5 to 8 digits):
            1-416-81-2878-3-4-5999-6-7.....8-9-9-789
            the rule is:

            before the second dash, it's area code,
            and area code itself varies, for instance,
            (US--1,UK---->44, HK--->512?)
            after the last dash, it's extention number.

            how to do?
            • first at all, you have to clearly know what client will input and what client have to input (client side restrict), where and how to store these data (server side data structure),
              if your data structure is normalized, you will not have that kind of mess.
              • right, make a rule 先。可以用input mark来做一些限制。还是你说你的数据已经是这样的mess了?
                • It's even worse....
                  the real data is like
                  1-416-123*-887-*9877@888-99908

                  one thing I'm sure is before 416 is area code and after the final
                  dash, it's extention.

                  it's transfered from a legacy system and I got only
                  a txt file contians all these numbers.

                  any solutions?
                  • 记得几年前用过QE(QUICKEDIT)有列编辑功能, 对于这种文本特有用,然后BCP.没看到你的格式,没发给意见
                    • kidding me?
                      It's transfered from a database, Megabytes level,
                      any editor can deal with that?

                      the typical formats are
                      1) 1-416-512*187-0-999--->should be (1)416-5121780ext999
                      2) 44-321-1&85-888---->should be (44)321-185ext888
                      3) 1-416-6658790-8879--->should be (1)416-6658790ext8879

                      all non-nummeric characters in the middle should removed
                      • use Replace() function, refer to MSDN
                        • I tried before, no use...........
                      • may
                        get the data part before first dash, delete Non-numeric, you got (1)
                        get the data after the last dash, delete non-numeric, you got ext99908
                        get the middle part
                        get the data part before first dash, delete Non-numeric, you got 416-
                        delele Non-numeric among the data after dash, you got 123887
                        • logic clear, but....
                          but imagine how may instr(), mid() you will use
                          and you definitely need a loop, it's not clear.
                          The solution I saw before is just about 5 to 6 lines
                          no loop.
                          • I 服了U. Spending 5 hours waiting/searching for a 5-6 lines solution, but do not want to write 20 lines of codes.
                      • 如果你可以肯定要找的是“-”的话,可以用instr()找出两个“-”的位置,然后用substr()把原来的string分成3段,把中间的那段处理成全部是数字的string,然后再接起来。
                        • look at my post above
                          • 你会用instr()吗?instr()返回该字符在String中的位置。我就是看了你的post才说用instr()的,否则hellangel(地狱天使)的方法最简单。
                            • 他只是要5-6 行CODE
                  • sed & awk (or Perl) should be the best tools for this kind of task. But it's unix centric and has a bit of learning curve.
    • Hope it's helpful
      sNumber = Split(sVariable, "-")

      If UBound(sNumber) > 1 Then
      sVariable = sNumber(0) & "-"
      For i = 1 To UBound(sNumber)
      sVariable = sVariable & sNumber(i)
      Next
      End If
    • you have to be assuming that all the phone numbers are valid, then simply remove all the non-numeric charactors first, then reformat it based on the length of the remains, say, 10=3-7;
      13=3-7ext3; 11=1-800-7 etc. and so on