Reputation: 26538
I have a string as under
var input= "dbo.xyx.v1.UserDefinedFunction";
The desired output will be "xyx.v1";
My attempt is
var input = "dbo.xyx.v1.UserDefinedFunction";
var intermediate = input.Split('.').Skip(1);
var res = intermediate.Reverse().Skip(1).Aggregate((a, b) => b + "." + a);
works fine..but any other proper and elegant method?
Kindly note that it can be any part(in the example I just showed 4 parts)
e.g. Input : "dbo.part1.part2.part3.part4.UserDefinedFunction" Output : "part1.part2.part3.part4"
Upvotes: 4
Views: 118
Reputation: 727067
If you need to use LINQ, you can use Skip(1).Take(2)
and string.Join
, like this:
var parts = input.Split('.');
var res = string.Join(".", parts.Skip(1).Take(parts.Length-2));
If you need to throw away the first and the last parts, then you can use Substring
, like this:
var start = input.IndexOf('.')+1;
var end = input.LastIndexOf('.')-1;
var res = input.Substring(start, end-start+1);
Finally, you can use regular expression, like this:
var res = Regex.Replace(input, "^[^.]+[.](.+)[.][^.]+$", "$1");
Upvotes: 1
Reputation: 460328
This returns always the mid part of a string which can be one or two parts(acc. to the total 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)));
So in this case it returns xyx.v1
, for a string bo.xyx.v1
it returns v1
since that's the only mid-part.
Upvotes: 2
Reputation: 11075
string output= input.Substring(input.IndexOf('.') + 1,
input.LastIndexOf('.') - input.IndexOf('.') - 1);
Upvotes: 1
Reputation: 19473
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;
}
Upvotes: 1
Reputation: 325
[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;
}
}
Upvotes: 0
Reputation: 32511
var input = "dbo.xyx.v1.UserDefinedFunction";
var intermediate = input.Split('.');
var res = string.Join(".", intermediate[1],intermediate[2]);
EDIT: for any part version
var res = string.Join(".", intermediate.Skip(1).Take(intermediate.Length - 2));
Upvotes: 0
Reputation: 70796
You could simplify it and do:
var split = input.Split(".");
var result = String.Join(".", split[1], split[2]);
No need for Skip
or Take
.
Upvotes: 0
Reputation: 1039498
var input = "dbo.xyx.v1.UserDefinedFunction";
var res = string.Join(".", input.Split('.').Skip(1).Take(2));
Upvotes: 0