2

I've got a list of strings that look like this

String s = "Top-0001-V[5]"

and I need to switch the parts to this Pattern:

String sF = "0001-Top-V-[5]"

I already tried to split them at "-" and then adding them, but the problem is it's a really long line of code, is there a way of doing this in an easy way, or do i have to split it all the way up and add it back togehther?

3
  • 1
    What is your split pattern exactly? I couldn't get it. Commented Jan 19, 2016 at 8:26
  • 2
    Really long line of code? - how long? why so? why do you consider it too long? Commented Jan 19, 2016 at 8:28
  • I think it's <1>-<2>-<3>[<4>], where <1> should be swapped with <2> and between <3> and [<4>] should be a -. Commented Jan 19, 2016 at 8:29

2 Answers 2

3

This might do the trick for you

var items = s.Split('-');
items[items.Length - 1] =  items[items.Length - 1].Replace("[","-[");
string x = String.Format("{0}-{1}-{2}", items[1], items[0], items[2]);

The output will be

0001-Top-V-[5]

You can also use String.Substring if the format of the string is common because String.Substring Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

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

2 Comments

I would add .Replace("[", "-[") at the end of the string format to get 0001-Top-V-[5].
Its pleasure @WinnieTopGun. Happy Coding :)
3

A Regex might help:

var r = new Regex("(Top)-(\\d+)-(V)\\[(\\d+)\\]");
string result = r.Replace(myInout, "$2-$1-$3-[$4]")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.