Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# - string replace helper

Posted on 2011-09-22
3
Medium Priority
?
326 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
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

783 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