How can I parse out some items from a string in c# - c#

I have strings like this:
/Administration/References
/Administration/Menus/Home
etc
Is there an easy way that I can find the 1st, 2nd and 3rd words that appear in these strings and place it into an array. ie. the text between the slashes?

The easiest way in this case is
var words = myString.Split(new[]{'/'}, StringSplitOptions.RemoveEmptyEntries);
This will give you an array of all the words seperated by the slashes.
The StringSplitOptions.RemoveEmptyEntries will make sure that you don't get empty entries, since the string is starting with a / it will give an empty first element in the array. If you have a trailing / it will give a empty last element as well.

string.Split(new char[] { '/' })
See MSDN for more info:
http://msdn.microsoft.com/en-us/library/b873y76a.aspx

I think what you are looking for is the split method on string i.e.
string[] words = yourstring.Split('/');

It will give you a List that contains 1st line, 2nd line and etc. Each list item is an Array of strings that you want to parse.
private List<string[]> ParseText(string text)
{
string[] lines = text.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
List<string[]> list = new List<string[]>();
foreach (var item in lines)
{
list.Add(item.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries));
}
return list;
}

Related

Splitting the line in a CSV using C#

I am reading a CSV file and a line reads like this
Context Tenant: {Vendor: 1, Customer: 719046046}","1,664,705.67","","7/11/2017 4:55 PM"
Now i would like to split this and collect each values in a Array.
value[0] should be Context Tenant: {Vendor: 1, Customer: 719046046}
value[1] should be 1,664,705.67
value[2] should be 7/11/2017 4:55 PM.
Tried using Regex and Split
var values = line1.Split(new char[] { '\\', '"' ,','}, StringSplitOptions.RemoveEmptyEntries);
var values = Regex.Split(line1, "\\,");
However i am unable to read the value as i expected. Can you please share some light.
this should do the job. This version will remove the empty spaces
string [] n_new = g.Split(new string[] { "\",\""}, StringSplitOptions.RemoveEmptyEntries)
To get rid of the starting end end " you can put an additional Trim before the Split:
string [] ret = g.Trim('"').Split(new string[] { "\",\""}, StringSplitOptions.RemoveEmptyEntries);
Because some of your elements contains commas I'd suggest splitting specifically on ",", so use this in your split statement: "\",\"". So you should end up with:
var values = Regex.Split(line1, "\",\"");
And then to solve your issues of including " in the results, consider:
foreach (string value in string[] values)
value = value.Replace("\"\"", "");
to get rid of all the unwanted extra double quotes ("). Sort of hacky but it should do the trick.

How do I split a string on both commas and newlines in c#?

I'm trying to split a string that can come in with either commas or newlines, based on an input from a textarea. I'm not sure of the syntax to split this string in c#.
Currently I have:
string[] splitString = inputString.Split(','); //WORKS
//string[] splitString = inputString.Split(new string[] { ",","\r\n","\n" }, StringSplitOptions.None); //DOES NOT WORK
Since some text uses \r for new line.
You should use the code below and remove the empty entries to make the array cleaner.
string[] splitString = inputString.Split(new string[] { ",", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
or using Regex.Split. (This doesn't remove empty entries.)
String[] splitString = Regex.Split(inputString, "[,\r\n]");
Update
You can also use Regex.Split with empty entries removed, thanks to WiktorStribiżew's comment.
The code below removes the empty entries which aren't in the beginning or end of the string.
String[] splitString = Regex.Split(inputString, "[,\r\n]+");
To eliminate empty entries showing in the beginning or end of the line, use the code below.
Regex.Split(Regex.Replace(inputString, "^[,\r\n]+|[,\r\n]+$", ""), "[,\r\n]+");
Regular Expression Language
If you want more informations about Regex, or how it works, you can look here for a quick reference.
You can pass Environment.NewLine into your string array:
string[] splitString = inputString.Split(new string[] { ",", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

Why the Split function has two array?

When i program with C#,using Split function:
string[] singleStr=str.Split(';');
the str is column111.dwg&186&0;
Why the singleStr.Length=2? Why give a array althrough the array is null?
I'm not sure what desStr looks like but sounds like you need to use StringSplitOptions.RemoveEmptyEntries
The return value does not include array elements that contain an empty
string
string str = "column111.dwg&186&0;";
string[] singleStr = str.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in singleStr)
{
Console.WriteLine(item);
}
Output will be only;
column111.dwg&186&0
Here a demonstration
If we don't use StringSplitOptions.RemoveEmptyEntries in this case, singleStr array has 2 items; column111.dwg&186&0 and ""
If you dont want to have empty entries, use this construction:
string[] singleStr=str.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries);
Basically, it's correct to have two items in your array as you do have a semicolon in your string.
One of the prototypes of Split method allow you to set the SplitStringOptions to RemoveEmptyEntries
Eg:
var parts = yourString.Split( new []{';'}, SplitStringOptions.RemoveEmptyEntries);
Use this:
string[] singleStr=str.Split(new[]{';'}, StringSplitOptions.RemoveEmptyEntries);
it will remove null's and empty strings like whitespaces and others
Answer to your question:
array length is 2 because Split sees: column111.dwg&186&0; and do split on ; and gets:
column111.dwg&186&0 and after ; it has null string only
If you wish to ignore empty entries, try using
String.Split Method (Char[], StringSplitOptions)
Returns a string array that contains the substrings in this string
that are delimited by elements of a specified Unicode character array.
A parameter specifies whether to return empty array elements.
StringSplitOptions Enumeration
RemoveEmptyEntries:
The return value does not include array elements that contain an empty
string
This is because it splits the string everywhere it finds a ";" in the string and that results in an empty entry because your ";" is at the end of the string.
You can use following call to remove emtpy entries:
str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

How do I know which delimiter was used when delimiting a string on multiple delimiters? (C#)

I read strings from a file and they come in various styles:
item0 item1 item2
item0,item1,item2
item0_item1_item2
I split them like this:
string[] split_line = line[i].split(new char[] {' ',',','_'});
I change an item (column) and then i stitch the strings back together using string builder.
But now when putting the string back I have to use the right delimiter.
Is it possible to know which delimiter was used when splitting the string?
UPDATE
the caller will pass me the first item so that I only change that line.
Unless you keep track of splitting action (one at the time) you don't.
Otherwise, you could create a regular expression, to catch the item and the delimiter and go from there.
Instead of passing in an array of characters, you can use a Regex to split the string instead. The advantage of doing this, is that you can capture the splitting character. Regex.Split will insert any captures between elements in the array like so:
string[] space = Regex.Split("123 456 789", #"([,_ ])");
// Results in { "123", " ", "456", " ", "789" }
string[] comma = Regex.Split("123,456,789", #"([,_ ])");
// Results in { "123", ",", "456", ",", "789" }
string[] underscore = Regex.Split("123_456_789", #"([,_ ])");
// Results in { "123", "_", "456", "_", "789" }
Then you can edit all items in the array with something like
for (int x = 0; x < space.Length; x += 2)
space[x] = space[x] + "x";
Console.WriteLine(String.Join("", space));
// Will print: 123x 456x 789x
One thing to be wary of when dealing with multiple separators is if there are any lines that have spaces, commas and underscores in them. e.g.
37,hello world,238_3
This code will preserve all the distinct separators but your results might not be expected. e.g. the output of the above would be:
37x,hellox worldx,238x_3x
As I mentioned that the caller passes me the first item so I tried something like this:
// find the right row
if (lines[i].ToLower().StartsWith(rowID))
{
// we have to know which delim was used to split the string since this will be
// used when stitching back the string together.
for (int delim = 0; delim < delims.Length; delim++)
{
// we split the line into an array and then use the array index as our column index
split_line = lines[i].Trim().Split(delims[delim]);
// we found the right delim
if (split_line.Length > 1)
{
delim_used = delims[delim];
break;
}
}
}
basically I iterate each line over the delims and check the resulting array length. If it is > 1 that means that delim worked otherwise skip to next one. I am using split functions property "If this instance does not contain any of the characters in separator, the returned array consists of a single element that contains this instance."

Separating a string into substrings

I want to separate a string consisting of one or more two-letter codes separated by commas into two-letter substrings and put them in a string array or other suitable data structure. The result is at one point to be databound to a combo box so this needs to be taken into account.
The string I want to manipulate can either be empty, consist of two letters only or be made up by multiple two-letter codes separated by commas (and possibly a space).
I was thinking of using a simple string array but I'm not sure if this is the best way to go.
So... what data structure would you recommend that I use and how would you implement it?
Definitely at least start with a string array, because it's the return type of string.Split():
string MyCodes = "AB,BC,CD";
char[] delimiters = new char[] {',', ' '};
string[] codes = MyCodes.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
Update: added space to the delimiters. That will have the effect of trimming spaces from your result strings.
Would something like this work?
var list = theString.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
My answer is "right", but I suggest Joel Coehoorn's answer.
public static string[] splitItems(string inp)
{
if(inp.Length == 0)
return new string[0];
else
return inp.Split(',');
}
If you are simply going to bind to the structure then a String[] ought to be fine - if you need to work with the data before you use it as a data source then a List<String> is probably a better choice.
Here is an example:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
String s = "ab,cd,ef";
// either a String[]
String[] array = s.Split(new Char[] {','});
// or a List<String>
List<String> list = new List<String>(s.Split(new Char[] { ',' }));
}
}

Categories

Resources