String.Split(), empty strings and method deleting specified characters
string.Split()
method:
" ".Split();
will result in an array with 2 string.Empty
items as there is nothing (empty) on either side of the space character.
" something".Split();
and "something ".Split();
will result in an array with two items, that one of them is an empty string, and actually one side of the space character is empty.
"a b".Split(); //double space in between
The first space has a
on the left side and an empty string on the right side (the right side is empty because there is another delimiter right after), the second space, has an empty string on the left side and b
on the right side. so the result will be:
{"a","","","b"}
How can I remove empty string from an array of string in C#?
You can use LINQ to filter out the empty entires:
using System.Linq;
...
string[] uorsList = uors.Split(';');
var filtered = uorsList.Where(s=> !string.IsNullOrWhiteSpace(s)).ToArray();
EDIT:
As pointed out in comments, when using string.Split
the following is a better option as the empty entries will never make it into the array:
string[] uorsList = uors.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
Splitting a string on a space and removing empty entries
string[] elements = templine.Split(space).Where((s, i) => i != 1).ToArray();
C#: splitting a string and not returning empty string
String.Split takes an array when including any StringSplitOptions
:
string[] batchstring = batch_idTextBox.Text.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries);
If you don't need options, the syntax becomes easier:
string[] batchstring = batch_idTextBox.Text.Split(';');
Getting rid of null/empty string values in a C# array
Try this:
yourString.Split(new string[] {";"}, StringSplitOptions.RemoveEmptyEntries);
Split string to array, remove empty spaces
Add .Replace(" ","")
String tmp = @"abc 123 ""Edk k3"" String;";
var tmpList = tmp.Split(new[] { '"' }).SelectMany((s, i) =>
{
if (i % 2 == 1) return new[] { s.Replace(" ", "") };
return s.Split(new[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
}).ToList();
How to remove only last empty element from array after spliting in c#
You can use TrimEnd
method to remove whitespaces from end on the String
instance before calling Split
:
string input = "1,2,3, ,4, , ";
input.TrimEnd()
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
Output Array:
["1"," ","2","3"," ","4"," "]
See DEMO Fiddle here
Split a comma separated string while removing whitespace and empty entries
Using Trim with StringSplitOptions.RemoveEmptyEntries
doesn't work because " "
isn't considered an empty entry. You need to do a normal split, then trim each item, then filter out the empty strings.
valueString.Split(',')
.Select(x => x.Trim())
.Where(x => !string.IsNullOrWhiteSpace(x))
.ToArray();
C# Regex.Split: Removing empty results
Regex lineSplitter = new Regex(@"[\s*\*]*\|[\s*\*]*");
var columns = lineSplitter.Split(data).Where(s => s != String.Empty);
or you could simply do:
string[] columns = data.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
foreach (string c in columns) this.textBox1.Text += "[" + c.Trim(' ', '*') + "] " + "\r\n";
And no, there is no option to remove empty entries for RegEx.Split as is for String.Split.
You can also use matches.
Related Topics
Ef Code First - How to Set Identity Seed
Camera.Main Is Null When Performing Raycast
Advantages to Using Private Static Methods
How to Add a Line Break in C# .Net Documentation
How to Route Images Using ASP.NET MVC Routing
Fields of Class, Are They Stored in the Stack or Heap
How Do Closures Work Behind the Scenes? (C#)
C#7: Underscore ( _ ) & Star ( * ) in Out Variable
Split String into String Array of Single Characters
Convert from Scientific Notation String to Float in C#
Using Prepared Statement in C# with MySQL
Why Is Try {...} Finally {...} Good; Try {...} Catch{} Bad
What Is a Good Unique Pc Identifier