• (nodebb) in reply to hoser

    Of course, reinventing the wheel is always a good idea. Why use the built-in translate function if you can build it yourself?

  • Regurgitated rubbish (unregistered) in reply to The true WTF?

    And if you're so shallow that you need to be talked down from a ledge after seeing some clear, efficient, and ugly code,

    Yes, it passes unit testing and is obvious, so that's a tick. I'm old, I'll take working code over 'perfect' code any day.

    If I saw that in code review it'd raise an eyebrow and the offender told that's it's not the best (and you'd look a bit more closely at their other code). If it got flagged in performance testing because you need to process 'War & Peace' 20 times a second, then you'd fix it.

    It's just lazy-arse intern-level code, nothing to get excited about.

    One job had a senior & junior programmer spend all day writing a simple function that already was in the code library (and was better than what they came up with), plus the DBA's thought it a good idea to format combo box lists in the stored procedures... now there's a real WTF.

  • Boneist (unregistered) in reply to hoser

    Another Oracle PL/SQL developer here; I'd not use REPLACE or regular expressions here; I'd just go for translate(strText, '0123456789', '0000000000'). Bish, bash, bosh. wipes hands

  • Dave (unregistered) in reply to Regurgitated rubbish

    It's stated clearly in the article that there are only 'numbers' - digits - in the string. Although I suppose e is a number, so excellent pedantry.

  • (nodebb)

    I remember way back having to edit some VB 5 code. The reference I was using said something about the language's "rich library of string functions", but I couldn't find its Replace() function and ended up writing my own (that's what stuck with me in the years since: a "rich library of string functions" that didn't include search-and-replace - I was sure I was missing something).

    Having done that, if I had needed to make multiple replacements in one string I could very well have written something a lot like this (it's too long ago for me to remember).

  • Regurgitated rubbish (unregistered) in reply to Watson

    but I couldn't find its Replace() function and ended up writing my own

    That vaguely rings a bell, I think VB6 added a bunch of string stuff. Anyway, I do remember replace() was very slow so writing your own was a good idea.

  • Object delete. (unregistered)

    The real WTF here is the use of VB.

    It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.

    E.W. Dijkstra, 1975: How do we tell truths that might hurt?

  • Harrow (unregistered)

    For all that Jane or her co-workers know, the original author may have tried a couple of different looping solutions and some kind of regex call, and discovered that the nested replace call technique was significantly faster than anything else.

    Talk to me when you've coded a more efficient solution and demonstrated the improvement.

  • that'snotawtf (unregistered)

    Eh, I personally prefer the look of: Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(strText, "9", "8"), "8", "7"), "7", "6"), "6", "5"), "5", "4"), "4", "3"), "3", "2"), "2", "1"), "1", "0") But as said before: perfectly clear and serviceable code.

  • Pewe (unregistered)

    Are we doing stupid ways to solve problem X? Function ReplaceNumbersWith0(in as String) as string for i = 1 to 9 in = Replace(in, cstr(i), "0") next Return in end function

  • Helten (unregistered) in reply to death the kid

    I'm afraid that wouldn't compile.

  • Regurgitated rubbish (unregistered) in reply to Harrow

    Talk to me when you've coded a more efficient solution and demonstrated the improvement.

    Been there & got the t-shirt years ago.

    Replace() in VB is very slow, like most of the string functions. When called it makes a copy of the string, processes it and copies it back. (In VB parlance ByValue rather then ByReference aka pointer). With 9 cascaded calls that's a lot of overhead passing string back & forth.

    The code examples posted that do Mid$(String, Pos, 1) = "0" modify the string in place and are faster then the native function. For short strings it make not be worth bothering about though.

  • Cynic Missing Tables (unregistered)

    In C# strings are immutable types....

    This is also efficient for larger strings.

        public string MaskNumbers(string input)
        {
            if (input == null) return null;
            if (input == string.Empty) return string.Empty;
    
            // reserve the memory we need
            var resultBuilder = new System.Text.StringBuilder(input.Length);
            foreach (int character in input)
            {
                if (character > 47 && character < 58)
                {
                    resultBuilder.Append(0);
                }
                else
                {
                    resultBuilder.Append((char)character);
                }
            }
            return resultBuilder.ToString();
        }
    
  • hoser (unregistered) in reply to nerd4sale

    I'll concede that I forgot about using TRANSLATE when I posted. In this case it'll work fine. Most of our cases we aren't doing a one-to-one replacement, we're replacing a multiple-character pattern, like chr(13) || chr(10) with ",", so usually REPLACE is the simplest way to handle it.

    The code in the submission isn't the best, but it's by no means terrible. Just ugly. And let he who has not written ugly code cast the first stone.

  • Anon (unregistered) in reply to Dave

    [quote=Dave]It's stated clearly in the article that there are only 'numbers' [/quote]

    No it is not! Did you read the article?

  • Regurgitated rubbish (unregistered) in reply to Dave

    so excellent pedantry.

    Well Dave, if you want excellent pedantry I'll refer you to the (mercifully short) article that starts:

    You have a string. It contains numbers.

    Note that it doesn't say 'only' numbers. Nor does it say 'positive integers' either, so decimal points, commas & negative signs may be in there, possibly even parentheses & dollar signs.

    More wary folk will say "Numbers eh? What sort of numbers?" Call us pedantic if you like, but our code will pass testing better than yours.

  • death the kid (unregistered) in reply to Helten

    I got code from my working Test Java Project...

    The main point was in using Algorithm for the Problem...

  • Nitpicker (unregistered)

    The real WTF is that the correct English isn't "replace ... with", it's "replace ... by".

  • vbcbcv (unregistered)

    The code might be ugly looking, but it is simple. If it's fast enough then I don't see a wtf?

Leave a comment on “Got Your Number”

Log In or post as a guest

Replying to comment #:

« Return to Article