• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 69
  • Last Modified:

LINQ # - How to split and get value based on my count

Hi,

How to split and get value based on my count.

For example,

I have inputcount = 2
MyString = "ABC_21-NIM-3547-789_23-278";

i want to get the final string is "ABC_21-NIM". because my input is "2". so i want to split("-") and get the values.

so final output after   splitting
 ABC_21-NIM ( here ABC_21 is first string and NIM is second string)

how to form the values using LINQ C#.?
0
Ganesh S
Asked:
Ganesh S
  • 2
1 Solution
 
Pawan KumarDatabase ExpertCommented:
Please use this -

We can use simple string.split method to get this.

--

string MyString = "ABC_21-NIM-3547-789_23-278";
string[] intime = MyString.Split(new char[] { '-' },StringSplitOptions.RemoveEmptyEntries);
string finalstring = string.Join("-", intime.Take(2));

--

Open in new window


OUTPUT

--

ABC_21-NIM

--

Open in new window

0
 
Ganesh STech Lead cum developerAuthor Commented:
really great help..thanks..
0
 
Ganesh STech Lead cum developerAuthor Commented:
good solution..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now