How to get the intermediate part of a string?

I have a line under

var input= "dbo.xyx.v1.UserDefinedFunction"; 

The required output will be "xyx.v1";

My attempt

 var input = "dbo.xyx.v1.UserDefinedFunction"; var intermediate = input.Split('.').Skip(1); var res = intermediate.Reverse().Skip(1).Aggregate((a, b) => b + "." + a); 

works great .. but is there any other correct and elegant method?

Please note that this can be any part (in the example, I just showed 4 parts)

eg. Input: "dbo.part1.part2.part3.part4.UserDefinedFunction" Output: "part1.part2.part3.part4"

+4
source share
8 answers
  string output= input.Substring(input.IndexOf('.') + 1, input.LastIndexOf('.') - input.IndexOf('.') - 1); 
+1
source

This always returns the middle part of the string, which can be one or two parts (according to the common part number).

 var input = "dbo.xyx.v1.UserDefinedFunction"; string[] tokens = input.Split('.'); int midIndex = (tokens.Length - 1) / 2; IEnumerable<int> midIndices = midIndex % 2 == 0 ? new[] { midIndex } : new[] { midIndex, midIndex + 1 }; string mid = string.Join(".", tokens.Where((t, i) => midIndices.Contains(i))); 

Demo

So, in this case, it returns xyx.v1 , for the string bo.xyx.v1 it returns v1 , since only the middle part.

+2
source

If you need to use LINQ, you can use Skip(1).Take(2) and string.Join , for example:

 var parts = input.Split('.'); var res = string.Join(".", parts.Skip(1).Take(parts.Length-2)); 

If you need to throw away the first and last parts, you can use Substring , for example:

 var start = input.IndexOf('.')+1; var end = input.LastIndexOf('.')-1; var res = input.Substring(start, end-start+1); 

Finally, you can use a regular expression, for example:

 var res = Regex.Replace(input, "^[^.]+[.](.+)[.][^.]+$", "$1"); 
+1
source
 var input = "dbo.xyx.v1.UserDefinedFunction"; var res = string.Join(".", input.Split('.').Skip(1).Take(2)); 
+1
source
 var input = "dbo.xyx.v1.UserDefinedFunction"; var start = input.IndexOf('.'); var end = input.LastIndexOf('.'); string output; if (start < end) { output = input.Substring(start+1, end-start-1); } else { output = input; } 
+1
source
 var input = "dbo.xyx.v1.UserDefinedFunction"; var intermediate = input.Split('.'); var res = string.Join(".", intermediate[1],intermediate[2]); 

EDIT: for any version of the part

 var res = string.Join(".", intermediate.Skip(1).Take(intermediate.Length - 2)); 
0
source

You can simplify it and do:

 var split = input.Split("."); var result = String.Join(".", split[1], split[2]); 

No need for Skip or Take .

0
source
 [TestClass] public class UnitTest2 { [TestMethod] public void TestMethod1() { var ret = "this.is.my.test.string".MySplit(".", new int[] {0,1,4 });//this.is.string } } public static class Process { public static string MySplit(this string Source, string seprator, int[] positionTokeep) { var items = Source.Split(seprator.ToCharArray()); string ret = string.Empty; for (int i = 0; i < positionTokeep.Length; i++) { ret += items[positionTokeep[i]] + seprator; } if (!string.IsNullOrWhiteSpace(ret)) { ret = ret.Substring(0,ret.Length - seprator.Length); } return ret; } } 
0
source

All Articles