2

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.

2
  • Your examples seem to be replacing the sub-string between the two hyphens (-); this does does not match your description of what you want. Commented Apr 2, 2012 at 9:10
  • See the answer to this question Commented Apr 2, 2012 at 9:11

4 Answers 4

3

If you want to replace the first ":Number-" from the source with the content from target, you can use the following regex.

 var pattern1 = New Regex(":\d{1,3}-{1}"); if(pattern1.IsMatch(source) && pattern1.IsMatch(target)) { var source = "tcm:7-426-8"; var target = "tcm:10-15-2"; var res = pattern1.Replace(source, pattern1.Match(target).Value); // "tcm:10-426-8" } 

Edit: To not have your string replaced with something empty, add an if-clause before the actualy replacing.

Sign up to request clarification or add additional context in comments.

2 Comments

what if there's no match in target?
Then, the source match will be replaced with an empty string. Added some check before the regex. Thanks for the hint- input validation wasn't in my scope.
0

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")); } 

Comments

0

I'm not clear on the logic used to decide which bit from which string is used, but still, you should use Split(), rather than mucking about with string offsets: (note that the Remove(0,4) is there to remove the tcm: prefix)

string[] source = "tcm:90-2-10".Remove(0,4).Split('-'); string[] target = "tcm:42-23-17".Remove(0,4).Split('-'); 

Now you have the numbers from both source and target in easy-to-access arrays, so you can build the new string any way you want:

string output = string.Format("tcm:{0}-{1}-{2}", source[0], target[1], source[2]); 

2 Comments

Based on the examples the code should be string output = string.Format("tcm:{0}-{1}-{2}", target[0], source[1], source[2]);
Possibly. It's a bit vague, and the three examples don't match. But it doesn't matter - I'm not here to give the OP the exact method to copy-paste into his code, but to give a possible solution.
0

Heres without regex

string source = "tcm:7-426-8"; string target = "tcm:10-15-2"; int targetBeginning = target.IndexOf("-"); int sourceBeginning = source.IndexOf("-"); string temp = target.Substring(0, targetBeginning);//tcm:10 string result = temp + source.Substring(sourceBeginning, source.Length-sourceBeginning); //tcm:10 + -426-8 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.