priyanka.sarkar
priyanka.sarkar

Reputation: 26538

How to get the intermediate portion of string?

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

Answers (8)

Sergey Kalinichenko
Sergey Kalinichenko

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

Tim Schmelter
Tim Schmelter

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)));

Demo

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

Victor Mukherjee
Victor Mukherjee

Reputation: 11075

   string output= input.Substring(input.IndexOf('.') + 1, 
                  input.LastIndexOf('.') - input.IndexOf('.') - 1);

Upvotes: 1

Roee Gavirel
Roee Gavirel

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

Eugene
Eugene

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

Hossein Narimani Rad
Hossein Narimani Rad

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

Darren
Darren

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

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

var input = "dbo.xyx.v1.UserDefinedFunction";
var res = string.Join(".", input.Split('.').Skip(1).Take(2));

Upvotes: 0

Related Questions