Solved

C# - string replace helper

Posted on 2011-09-22
3
306 Views
Last Modified: 2012-05-12
Hello Experts...

I need help converting a string from one format to another. So, I have a bunch of xml xpath values in a datatable for processing later, for example one looks like this:
/soap:Envelop/soap:Body/ns2:findAllRolesResponse/return[2]/unscheduledWorkTOs[3]/workRangeTo/workRangeStartNumber

What I need is a little help doing a string replace so that the corresponding string looks like this one:

/*[local-name(.)='Envelope'][1]/*[local-name(.)='Body'][1]/*[local-name(.)='findAllRolesResponse'][1]/*[local-name(.)='return'][2]/*[local-name(.)='unscheduledWorkTOs'][3]/*[local-name(.)='workRangeTo'][1]/*[local-name(.)='workRangeStartNumber'][1]

I can get the first of the the string replaced using StringBuilder Replace...see attached code.

Just need help crawling through the rest of the string to change it as well.


        Any help is much appreciated.
Thanks!

static void Main(string[] args)
{
   string s = "/soap:Envelop/soap:Body/ns2:findAllRolesResponse/return[2]/unscheduledWorkTOs[3]/workRangeTo/workRangeStartNumber";

   // A
   // Create new StringBuilder from string
   StringBuilder b = new StringBuilder(s);
   Console.WriteLine(b);

   // B
   // Replace the first part
   b.Replace("/soap:Envelop/soap:Body/ns2:", "/*[local-name(.)='Envelope'][1]/*[local-name(.)='Body'][1]/*[local-name(.)='");
   Console.WriteLine(b);

}

Open in new window

0
Comment
Question by:j420exe1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 36584929
Use String.Split() to break the original string, on the '/' character, into an array of strings.  Loop through the array and remove everything up to and including the first colon - then append the result of that operation to the StringBuilder with the additional /*[local-name(.)='blah blah'][1] stuff.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
	static void Main(string[] args)
	{
		string testString = "/soap:Envelop/soap:Body/ns2:findAllRolesResponse/return[2]/unscheduledWorkTOs[3]/workRangeTo/workRangeStartNumber";
		StringBuilder output = new StringBuilder();

		// Split string on '/'
		string[] parts = testString.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

		// Loop through each part
		foreach (string part in parts)
		{
			// Remove everything up to and including the first colon
			string temp = part.Substring(part.IndexOf(':') + 1);

			// Append /*[local-name(.)='<part here>'[1] to the string builder
			output.AppendFormat("/*[local-name(.)='{0}'[1]", temp);
		}

		Console.WriteLine("Input:");
		Console.WriteLine(testString);
		Console.WriteLine();

		Console.WriteLine("Output:");
		Console.WriteLine(output.ToString());
		Console.WriteLine();

		Console.Write("Press any key to exit...");
		Console.ReadKey();
	}
}

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 36584934
I think my format on line 23 is a little wrong - just change it as-needed.
0
 

Author Closing Comment

by:j420exe1
ID: 36587060
Thanks...that worked and it wasn't as bad as I thought.

I updated line 23:
output.AppendFormat("/*[local-name(.)='{0}'", temp);
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question