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 173713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:23:31+00:00 2026-05-11T13:23:31+00:00

I’m trying to obfuscate a large amount of data. I’ve created a list of

  • 0

I’m trying to obfuscate a large amount of data. I’ve created a list of words (tokens) which I want to replace and I am replacing the words one by one using the StringBuilder class, like so:

 var sb = new StringBuilder(one_MB_string);  foreach(var token in tokens)  {    sb.Replace(token, 'new string');  } 

It’s pretty slow! Are there any simple things that I can do to speed it up?

tokens is a list of about one thousand strings, each 5 to 15 characters in length.

  • 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. 2026-05-11T13:23:31+00:00Added an answer on May 11, 2026 at 1:23 pm

    Instead of doing replacements in a huge string (which means that you move around a lot of data), work through the string and replace a token at a time.

    Make a list containing the next index for each token, locate the token that is first, then copy the text up to the token to the result followed by the replacement for the token. Then check where the next occurance of that token is in the string to keep the list up to date. Repeat until there are no more tokens found, then copy the remaining text to the result.

    I made a simple test, and this method did 125000 replacements on a 1000000 character string in 208 milliseconds.

    Token and TokenList classes:

    public class Token {      public string Text { get; private set; }     public string Replacement { get; private set; }     public int Index { get; set; }      public Token(string text, string replacement) {         Text = text;         Replacement = replacement;     }  }  public class TokenList : List<Token>{      public void Add(string text, string replacement) {         Add(new Token(text, replacement));     }      private Token GetFirstToken() {         Token result = null;         int index = int.MaxValue;         foreach (Token token in this) {             if (token.Index != -1 && token.Index < index) {                 index = token.Index;                 result = token;             }         }         return result;     }      public string Replace(string text) {         StringBuilder result = new StringBuilder();         foreach (Token token in this) {             token.Index = text.IndexOf(token.Text);         }         int index = 0;         Token next;         while ((next = GetFirstToken()) != null) {             if (index < next.Index) {                 result.Append(text, index, next.Index - index);                 index = next.Index;             }             result.Append(next.Replacement);             index += next.Text.Length;             next.Index = text.IndexOf(next.Text, index);         }         if (index < text.Length) {             result.Append(text, index, text.Length - index);         }         return result.ToString();     }  } 

    Example of usage:

    string text =     'This is a text with some words that will be replaced by tokens.';  var tokens = new TokenList(); tokens.Add('text', 'TXT'); tokens.Add('words', 'WRD'); tokens.Add('replaced', 'RPL');  string result = tokens.Replace(text); Console.WriteLine(result); 

    Output:

    This is a TXT with some WRD that will be RPL by tokens. 

    Note: This code does not handle overlapping tokens. If you for example have the tokens ‘pineapple’ and ‘apple’, the code doesn’t work properly.

    Edit:
    To make the code work with overlapping tokens, replace this line:

    next.Index = text.IndexOf(next.Text, index); 

    with this code:

    foreach (Token token in this) {     if (token.Index != -1 && token.Index < index) {         token.Index = text.IndexOf(token.Text, index);     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I would like to run a str_replace or preg_replace which looks for certain words
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.