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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:37:12+00:00 2026-06-01T06:37:12+00:00

I want to replace a charecter in a string with a string in c#.

  • 0

I want to replace a charecter in a string with a string in c#.
I have tried the following,

Here in the following program, i want replace set of charecters between charecters ‘:’ and first occurance of ‘-‘ with some others charecters.

I could able to extract the set of charecters between ‘:’ and first occurance of ‘-‘.

Can any one say how to insert these back in the source string.

        string source= "tcm:7-426-8";
        string target= "tcm:10-15-2";
        int fistunderscore = target.IndexOf("-");
        string temp = target.Substring(4, fistunderscore-4);
        Response.Write("<BR>"+"temp1:" +   temp + "<BR>");

Examples:

        source: "tcm:7-426-8" or "tcm:100-426-8" or "tcm:10-426-8"

        Target: "tcm:10-15-2" or "tcm:5-15-2" or "tcm:100-15-2"

        output: "tcm:10-426-8" or "tcm:5-426-8" or "tcm:100-426-8"

In a nutshell, I want to replace the set of charectes between ‘:’ and ‘-‘(firstoccurance) and the charecters extracetd from the same sort of string.

Can any help how it can be done.

Thank you.

  • 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-06-01T06:37:14+00:00Added an answer on June 1, 2026 at 6:37 am

    Try a regex solution – first this method, takes the source and target strings, and performs a regex replace on the first, targetting the first numbers after the ‘tcm’, which must be anchored to the start of the string. In the MatchEvaluator it executes the same regex again, but on the target string.

    static Regex rx = new Regex("(?<=^tcm:)[0-9]+", RegexOptions.Compiled);
    
    public string ReplaceOneWith(string source, string target)
    {
        return rx.Replace(source, new MatchEvaluator((Match m) =>
        {
            var targetMatch = rx.Match(target);
            if (targetMatch.Success)
                return targetMatch.Value;
            return m.Value; //don't replace if no match
        }));
    }
    

    Note that no replacement is performed if the regex doesn’t return a match on the target string.

    Now run this test (probably need to copy the above into the test class):

    [TestMethod]
    public void SO9973554()
    {
        Assert.AreEqual("tcm:10-426-8", ReplaceOneWith("tcm:7-426-8", "tcm:10-15-2"));
        Assert.AreEqual("tcm:5-426-8", ReplaceOneWith("tcm:100-426-8", "tcm:5-15-2"));
        Assert.AreEqual("tcm:100-426-8", ReplaceOneWith("tcm:10-426-8", "tcm:100-15-2"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that I want to replace the 'xx' with a line
I want to replace the numbers in the string with _number.We have to fetch
I have text data (in R) and want to replace some characters with other
I have the string page-24 and want to replace 24 by any other number.
i want to replace a character in the string with a string. can i
I want to replace with the 4~8 characters of a string with * ,how
I want to remove characters in a string in python: string.replace(',', '').replace(!, '').replace(:, '').replace(;,
I want to replace the last String which is a , with ) .
I want to replace each character of a string by a different one, shifted
I have a function which replace character. public static string Replace(string value) { value

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.