Feed aggregator
CodeSOD: Spaced Out Replacement
You have some text, and need to replace every sequence of spaces with a single space. E.g., My text becomes My text. Now, if you're most of us, you ignore the famous quote and reach straight for regexes.
But Samuel's co-worker isn't most of us.
toReturn = toReturn.Replace(" ", " "); toReturn = toReturn.Replace(" ", " ").Replace(" ", " ").Replace(" ", " ").Replace(" ", " ");What I love about this is you can see the programmer gradually understanding the problem better, right here in the code.
Now, this is C#, where Replace replaces all occurences.
They start with one line: if there are two spaces, replace them with one. Oops, except, they can't count, and they actually write instructions to replace one space with one space.
Okay, that didn't work, let's try again. Let's replace two spaces with one space. Hey, that's got us partway there, but if, for example, we've got three or four spaces, this still leaves us with two spaces. Okay, let's just add another replace. Great, but oh wait, what if we have six spaces? The first replace knocks us down to three, the second to two, so we need a third one.
And so this developer kept adding Replace calls until the spaces got down to the count they expected. Here's hoping they never get a string with more contiguous spaces than this block can handle.
Then again, what's the worst that happens? They just need to add another Replace(" ", " "). Easy.
[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.How 'Star Wars' was Influenced by San Francisco - and Architecture
Read more of this story at Slashdot.
Scientists Find a 'Missing Link' Between Poor Diet and Higher Cancer Risk
Read more of this story at Slashdot.
Microsoft's 'Responsible AI' Chief Worries About the Open Web
Read more of this story at Slashdot.
Can Technology Help Reduce Drunk-Driving Deaths?
Read more of this story at Slashdot.
America's Federal Regulators Are Preparing More Lawsuits Against Crypto Companies
Read more of this story at Slashdot.
Could Better Data Protections Reduce Big Tech's Polarizing Power?
Read more of this story at Slashdot.
Some San Francisco Tech Workers are Renting Cheap 'Bed Pods'
Read more of this story at Slashdot.
AI-Operated F-16 Jet Carries Air Force Official Into 550-MPH Aerial Combat Test
Read more of this story at Slashdot.
Will Calls to Scrutinize Digital-Currency Purchases of Oil Bring New Regulations For Crypto?
Read more of this story at Slashdot.
Is Mastodon's Link-Previewing Overloading Servers?
Read more of this story at Slashdot.
Plato's Final Hours Recounted In Scroll Found In Vesuvius Ash
Read more of this story at Slashdot.
Breakthrough Achieved In Nanometer-Resolution Imaging of 3D Chemistry
Read more of this story at Slashdot.
Microsoft Details How It's Developing AI Responsibly
Read more of this story at Slashdot.
The US Just Mandated Automated Emergency Braking Systems By 2029
Read more of this story at Slashdot.
AI-Powered 'HorseGPT' Fails to Predict This Year's Kentucky Derby Winner
Read more of this story at Slashdot.
U.S. Seeks to Build World Pressure on Russia Over Space Nuclear Weapon
Read more of this story at Slashdot.
Multinational ISP Offers $206M In Secured Notes Backed By IPv4 Addresses
Read more of this story at Slashdot.
Ubuntu Criticized For Bug Blocking Installation of<nobr> <wbr></nobr>.Deb Packages
Read more of this story at Slashdot.
NASA's Moon Capsule Suffered Extensive Damage During 2022's Test Flight
Read more of this story at Slashdot.