Unable to split a string using split() method - c#

I have a string
String data="CE|2014-2015|ClassA"
I need output like
string Batch="2014-2015"
string Class="ClassA"
How can I achieve it?? I tried a lot string,Split() function. But I did not get expected output.Please help me out
I tried,
string s = "CE|2014-2015|Class1";
string[] words = s.Split('|| ');

This should work for you
string[] splitted = data.Split('|');
string Batch = splitted[1];
string Class = splitted[2];
Your solution is wrong because: '|| ' is not a valid char and it won't even compile. You should split on | and take second and third value from splitted values

You can do the following
string data = "CE|2014-2015|ClassA";
string[] split = data.Split('|');
string Batch=split[1];
string Class = split[2];
Hope it works for you.

Related

Get a string inside a string

string a = "100-0-6-7-6-10-8-" //and so on
/////////////////////////////////////////////
//my solution
char[] delimiterChars = {'-'};
string solution = "100-0-6-7-6-10-8-";
string[] words = solution.Split(delimiterChars,System.StringSplitOptions.RemoveEmptyEntries);
//ok so i encounter some problems when trying to overwrite 0s, and 10s,100s etc
//so when writing data to the "solution" string it should be
//"-100--0--6--7--6--10--8-" instead of "100-0-6-7-6-10-8-"
basically, I want to separate each number and put it in a list or array
i think i got it
char[] delimiterChars = {'-'};
string text = "100-0-6-7-6-10-8-";
string[] words = text.Split(delimiterChars,System.StringSplitOptions.RemoveEmptyEntries);
This solution using Linq works as well.
var text="100-0-6-7-6-10-8-";
var words=text.Split("-").Where(x => !string.IsNullOrEmpty(x)).ToArray();

C#Regex Expression Getting certain words from a file path

Hey guys im writing a c# application and i cant seem to split up data using regex as id like to.
C:\Users\e014425c\Downloads\StoreData\ABE1102013.csv
Thats getting stored as a string within the loop. I want:-
ABE1
10
2013
From there i would store the individual strings into 3 arrays.
Problem is, within the loop the data will change so you cant use Contains("ABE1")
Help would be much appreciated!
foreach (string word in file)
{
string testString = word;
string firstWords = Regex.Match(testString, #"^(\w+\b.*?){6}").ToString();
Console.Write(firstWords);
}
Thats how i´ve been testing the data
First get the filename only from the path:
string filename = File.FilenameWithoutExtension(path);
Then the characters you want with SubString():
string a1 = filename.SubString(0, 4);
string a2 = filename.SubString(4, 2);
string a3 = filename.SubString(6, 4);
EDIT
With a general pattern "ABC1_xy_YYYY" it is better to use the underscores as delimiters:
string[] field = filename.Split('_');
a1 = field[0];
...

c# how to solve a issue using substring

How to solve substring issue. I have tried to code correctly but not working for me.
The file name is bad_filename.xml or good_filename.xml
what i want is to use substring to result "bad" or "good" where _filename.xml should be removed. how to do this?
From: bad_filename.xml or good_filename.xml
to: bad or good
Try this
string s = "bad_filename.xml";
string sub = s.Substring(0, s.IndexOf("_"));
string sub2 = string.Concat((s.TakeWhile(x => x != '_')));
string sub3 = s.Split('_')[0];
I've given three ways pick any one of your choice
Note: Way (1) will throw exception when string doesn't contain _ you need to check index > -1
Try this, as I have mention in Question comment.
var result = filename.Split('_')[0];
var result = filename.Split('_')[0];
Use the Path class to get the file name and string.Split to get the first part:
string fileNameWOE = Path.GetFileNameWithoutExtension("bad_filename.xml");
string firstPart = fileNameWOE.Split('_')[0];
try this
string input = "bad_filename.xml"
string sub = input.Substring(0, input.IndexOf("_"));
Console.WriteLine("Substring: {0}", sub);
You can use this code for substring.
string a="bad_filename.xml ";
int index=a.IndexOf('_');
if (index != -1)
{
string filename = a.Substring(0,index);
}
output is bad
do it like this :
string[] strArr = stringFileName.Split('_');
string[] strArr = bad_filename.xml.Split('_');
strArr[0] is "bad"
and
string[] strArr = good_filename.xml.Split('_');
strArr[0] is "good"

How to split string based on another string

I have a string:
string fileName = VAH007157100_REINSTMT_20d5fe49.tiff
I want to split this at the end of REINSTMT.
string splittedFileName = fileName.split("REINSTMT")[0];
The above does not work.
How would I go about splitting it to grab everything from the left side of the word "REINSTMT"?
Try this
string splittedFileName = fileName.Split(new string[]{"REINSTMT"},
StringSplitOptions.None)[0];
In order to split based on a string rather than a char, you need to provide a second argument. See the documentation here.
What you probably want is
string splittedFileName = fileName.split(new string[] {"REINSTMT"}, StringSplitOptions.None)[0];
Another way would be with substring:
string fileName = "VAH007157100_REINSTMT_20d5fe49.tiff";
string splittedFileName = fileName.Substring(0, fileName.IndexOf("REINSTMT"));

How to select only the characters after a repeated symbol in a long string

If i am using C# and i have a string coming in from a database like this:
\RBsDC\1031\2011\12\40\1031-215338-5DRH44PUEM2J51GRL7KNCIPV3N-META-ENG-22876500BBDE449FA54E7CF517B2863E.XML
And i only want this part of the string:
1031-215338-5DRH44PUEM2J51GRL7KNCIPV3N-META-ENG-22876500BBDE449FA54E7CF517B2863E.XML
How can i get this string if there is more than one "\" symbol?
You can use the LastIndexOf() method of the String class:
string s = #"\RBsDC\1031\2011\12\40\1031-215338-5DRH44PUEM2J51GRL7KNCIPV3N-META-ENG-22876500BBDE449FA.xml";
Console.Out.WriteLine(s.Substring(s.LastIndexOf('\\') + 1));
Hope, this helps.
Use String.Split to split string by parts and then get the last part.
Using LINQ Enumerable.Last() :
text.Split('\\').Last();
or
// todo: add null-empty checks, etcs
var parts = text.Split('\\');
strign lastPart = parts[parts.Length - 1];
You can use a combination of String.LastIndexOf("\") and String.Substring(lastIndex+1). You could also use (only in the sample you provided) Path.GetFileName(theString).
string[] x= line.Split('\');
string goal =x[x.Length-1];
but linq will be easier
You can use regex or split the string by "\" symbol and take the last element of array
using System.Linq;
public class Class1
{
public Class1()
{
string s =
#"\RBsDC\1031\2011\12\40\1031-215338-5DRH44PUEM2J51GRL7KNCIPV3N-META-ENG-22876500BBDE449FA54E7CF517B2863E.XML";
var array = s.Split('\\');
string value = array.Last();
}
}
newstring = string.Substring(string.LastIndexOf(#"\")+1);
It seems like original string is like filePath.
This could be one easy solution.
string file = #"\RBsDC\1031\2011\12\40\1031-215338-5DRH44PUEM2J51GRL7KNCIPV3N-META-ENG-22876500BBDE449FA.xml";
string name = System.IO.Path.GetFileName(file);

Categories

Resources