Disguise all characters in a string except the last n characters

I want to know how to replace a line character with the condition “except for the last character numbers”?

Example:

string = "4111111111111111"; 

And I want to do this so that

 new_string = "XXXXXXXXXXXXX1111" 

In this example, I replace the character with "X", except for the last 4 characters.

How can I achieve this?

+10
source share
8 answers

Here's a way to think it through. Call the last digits of the number to leave n :

  • How many characters will be replaced by X ? String length minus n .
  • How can we replace characters with other characters? You cannot directly modify string , but you can create a new one.
  • How to get the last n characters from the source string? There are several ways to do this, but the simplest one is Substring , which allows us to capture part of the line by specifying a start point and, optionally, an end point.

Thus, it will look something like this (where n is the number of characters that should leave the original, and str is the original string - string cannot be the name of your variable, because it is a reserved keyword):

 // 2. Start with a blank string var new_string = ""; // 1. Replace first Length - n characters with X for (var i = 0; i < str.Length - n; i++) new_string += "X"; // 3. Add in the last n characters from original string. new_string += str.Substring(str.Length - n); 
+7
source

Does this suit you?

 var input = "4111111111111111"; var length = input.Length; var result = new String('X', length - 4) + input.Substring(length - 4); Console.WriteLine(result); // Ouput: XXXXXXXXXXXX1111 
+21
source

How about something like ...

 new_string = new String('X', YourString.Length - 4) + YourString.Substring(YourString.Length - 4); 

create a new line based on the length of the current line -4, and just get all the "X". Then add the last 4 characters of the original string

+10
source
 StringBuilder sb = new StringBuilder(); Char[] stringChar = string.toCharArray(); for(int x = 0; x < stringChar.length-4; x++){ sb.append(stringChar[x]); } sb.append(string.substring(string.length()-4)); string = sb.toString(); 
+2
source

I think you could use Select with an index

 string input = "4111111111111111"; string new_string = new string(input.Select((c, i) => i < input.Length - 4 ? 'X' : c).ToArray()); 
+1
source

Lui, Please try this one ...

 string dispString = DisplayString("4111111111111111", 4); 

Create one function with the original pass line and without a digit.

  public string DisplayString(string strOriginal,int lastDigit) { string strResult = new String('X', strOriginal.Length - lastDigit) + strOriginal.Substring(strOriginal.Length - lastDigit); return strResult; } 

May help you ....

+1
source

Try this:

 String maskedString = "...."+ (testString.substring(testString.length() - 4, testString.length())); 
0
source

It may be a bit of Overkill at your request. But here is a quick extension method that does this.

x is used by default as a masking Char, but it can be changed with an extra character

  public static class Masking { public static string MaskAllButLast(this string input, int charsToDisplay, char maskingChar = 'x') { int charsToMask = input.Length - charsToDisplay; return charsToMask > 0 ? $"{new string(maskingChar, charsToMask)}{input.Substring(charsToMask)}" : input; } } 

Here are unit tests to prove it works.

  using Xunit; namespace Tests { public class MaskingTest { [Theory] [InlineData("ThisIsATest", 4, 'x', "xxxxxxxTest")] [InlineData("Test", 4, null, "Test")] [InlineData("ThisIsATest", 4, '*', "*******Test")] [InlineData("Test", 16, 'x', "Test")] [InlineData("Test", 0, 'y', "yyyy")] public void Testing_Masking(string input, int charToDisplay, char maskingChar, string expected) { //Act string actual = input.MaskAllButLast(charToDisplay, maskingChar); //Assert Assert.Equal(expected, actual); } } } 
0
source

All Articles