Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3213100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:54:25+00:00 2026-05-17T14:54:25+00:00

Using VB or C#, I am getting a string of variable length from the

  • 0

Using VB or C#, I am getting a string of variable length from the database. This information is sensitive information that only certain users will be able to see.

I have two cases that will use the same logic (I think).

scenario 1: replace all characters with x

scenario 2: replace all characters with x except the last 4 characters (assume length > 4 – this check is being done).

I thought that this would be easiest using Regex.Replace(input, pattern, replacestring). As opposed to a lot of string handling with substrings and forcing a length of ‘x’s.

But it seems that Regex will always be my kryptonite.

Any help from the regex gurus would be appreciated. Alternatively a better solution would be welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:54:25+00:00Added an answer on May 17, 2026 at 2:54 pm

    I’m not convinced that regular expressions are the best approach here, but these should work.

    ReplaceWithX replaces every single character (specified by .) with an x.

    ReplaceWithXLeave4 replaces all but the last four characters with an x. It does this by matching any single character (.) while using a zero-width negative lookahead assertion to throw out this match for the last four characters.

    using System;
    using System.Text.RegularExpressions;
    
    namespace ReplaceRegex
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(ReplaceWithX("12345678"));
                Console.WriteLine(ReplaceWithXLeave4("12345678"));
            }
    
            static string ReplaceWithX(string input)
            {
                return Regex.Replace(input, ".", "x");
            }
    
            static string ReplaceWithXLeave4(string input)
            {
                return Regex.Replace(input, ".(?!.{0,3}$)", "x");
            }
        }
    }
    

    And for completeness, below is what it looks like when not using regular expressions. This approach is probably quite a bit faster than the regex approach, even though you might not ever see the perf difference when just doing it once or twice like these examples are. In other words, if you’re doing this on a server with lots of requests, avoid regex since it’s only marginally easier to read.

    using System;
    using System.Text;
    
    namespace ReplaceNoRegex
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(ReplaceWithX("12345678"));
                Console.WriteLine(ReplaceWithXLeave4("12345678"));
            }
    
            static string ReplaceWithX(string input)
            {
                return Repeat('x', input.Length);
            }
    
            static string ReplaceWithXLeave4(string input)
            {
                if (input.Length <= 4)
                    return input;
    
                return Repeat('x', input.Length - 4)
                     + input.Substring(input.Length - 4);
            }
    
            static string Repeat(char c, int count)
            {
                StringBuilder repeat = new StringBuilder(count);
    
                for (int i = 0; i < count; ++i)
                    repeat.Append(c);
    
                return repeat.ToString();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone getting this error when using the new free chart controls MS bought from
Just getting started using MVC in ASP.NET, I'm going to have it so users
I'm getting the invalid lvalue error using gcc 3.4.6. The line that invokes the
Coming from a C background, I may be getting too anal about this and
I'm trying to load spring beans using XmlWebApplicationContext setConfigLocations method. However, I keep getting
Anybody tried here using Getting Real(37Signals) approach to develop windows application? (C#/.NET). Or simply
I'm getting the following error in Eclipse using the Cusp plug-in: Package LISP-UNIT is
I'm using Fedex's web services and getting an annoying error right up front before
I am getting the following error: Access denied for user 'apache'@'localhost' (using password: NO)
I am just getting started with flex and am using the SDK (not Flex

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.