How would you remove the blank item from the array?
Iterate and assign non-blank items to new array?
String test = "John, Jane";
//Without using the test.Replace(" ", "");
String[] toList = test.Split(',', ' ', ';');
Use the overload of string.Split that takes a StringSplitOptions:
String[] toList = test.Split(new []{',', ' ', ';'}, StringSplitOptions.RemoveEmptyEntries);
You would use the overload of string.Split which allows the suppression of empty items:
String test = "John, Jane";
String[] toList = test.Split(new char[] { ',', ' ', ';' },
StringSplitOptions.RemoveEmptyEntries);
Or even better, you wouldn't create a new array each time:
private static readonly char[] Delimiters = { ',', ' ', ';' };
// Alternatively, if you find it more readable...
// private static readonly char[] Delimiters = ", ;".ToCharArray();
...
String[] toList = test.Split(Delimiters, StringSplitOptions.RemoveEmptyEntries);
Split doesn't modify the list, so that should be fine.
string[] result = toList.Where(c => c != ' ').ToArray();
Try this out using a little LINQ:
var n = Array.FindAll(test, str => str.Trim() != string.Empty);
You can put them in a list then call the toArray method of the list, or with LINQ you could probably just select the non blank and do toArray.
If the separator is followed by a space, you can just include it in the separator:
String[] toList = test.Split(
new string[] { ", ", "; " },
StringSplitOptions.None
);
If the separator also occurs without the trailing space, you can include those too:
String[] toList = test.Split(
new string[] { ", ", "; ", ",", ";" },
StringSplitOptions.None
);
Note: If the string contains truely empty items, they will be preserved. I.e. "Dirk, , Arthur" will not give the same result as "Dirk, Arthur".
string[] toList = test.Split(',', ' ', ';').Where(v => !string.IsNullOrEmpty(v.Trim())).ToArray();
Related
I want to remove empty and null string in the split operation:
string number = "9811456789, ";
List<string> mobileNos = number.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(mobile => mobile.Trim()).ToList();
I tried this but this is not removing the empty space entry
var mobileNos = number.Replace(" ", "")
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
As I understand it can help to you;
string number = "9811456789, ";
List<string> mobileNos = number.Split(',').Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
the result only one element in list as [0] = "9811456789".
Hope it helps to you.
a string extension can do this in neat way as below
the extension :
public static IEnumerable<string> SplitAndTrim(this string value, params char[] separators)
{
Ensure.Argument.NotNull(value, "source");
return value.Trim().Split(separators, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim());
}
then you can use it with any string as below
char[] separator = { ' ', '-' };
var mobileNos = number.SplitAndTrim(separator);
I know it's an old question, but the following works just fine:
string number = "9811456789, ";
List<string> mobileNos = number.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
No need for extension methods or whatsoever.
"string,,,,string2".Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
return ["string"],["string2"]
The easiest and best solution is to use both StringSplitOptions.TrimEntries to trim the results and StringSplitOptions.RemoveEmptyEntries to remove empty entries, fed in through the pipe operator (|).
string number = "9811456789, ";
List<string> mobileNos = number
.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.ToList();
Checkout the below test results to compare how each option works,
I have a text file with the following information:
ALLOC
apple1
orange1
banana1
ALLOC
apple2
orange2
banana2
ALLOC
apple3
orange3
banana3
Based on the help from stackflow community, I am now able to read the whole file.I also found out that to extract contents between a tag, for ex, ALLOC, I could write:
var filelocation = #"c:\Fruits.txt";
var sectionLines = File.ReadAllLines(filelocation).TakeWhile(l => !l.StartsWith("ALLOC"));
But this will give me IEnumerable<string>:
apple1
orange1
banana1
apple2
orange2
banana2
apple3
orange3
How do I create 3 separate strings as
string1 = apple1 orange1 banana1
string2 = apple2 ornage2 banana2
string3 = apple3 orange3
In short, need to extract contents between tags.
Here is some approach how you can return result which you want:
string[] words = { "ALLOC", "apple1", "orange1", "banana1", "ALLOC", "apple2", "orange2", "banana2", "ALLOC" };
var result = string.Join(" ", words)
.Split(new string[] { "ALLOC" }, StringSplitOptions.RemoveEmptyEntries)
.Select(p => p.Trim(' '));
First I am making single string of all words. Than I am splitting by "ALLOC", and selecting trimmed strings.
Result is:
string[] result = { "apple1 orange1 banana1", "apple2 orange2 banana2" };
For your case,
var filelocation = #"c:\Fruits.txt";
var allLines = File.ReadAllLines(filelocation);
var sectionLines = string.Join(" ", allLines)
.Split(new string[] { "ALLOC" }, StringSplitOptions.RemoveEmptyEntries)
.Select(p => p.Trim(' '));
This might do the trick for you
string fullstr = File.ReadAllText("c:\\Fruits.txt");
string[] parts = fullstr.Split(new string[] { "ALLOC" }, StringSplitOptions.RemoveEmptyEntries);
List<string> outputstr = new List<string>();
foreach(string p in parts)
{
outputstr.Add(p.Replace("\r\n", " ").Trim(' '));
}
Here we read all text at once using File.ReadAllText and then splitted it with ALLOC and then in the outputstr just added the splitted string by replacing \r\n that is new line with a space and trimmed the result.
I have a text file where I have to split the values with every space (' ') and newline('\n'). It does not work very well since every newline has a carriage return connected to it (\r\n).
char[] param = new char[] {' ','\n','\r'}; // The issue
string[] input = fill.Split(param);
The param array does not accept a '\r\n' parameter as n split argument, that is why I used '\n' and '\r' separately, but it does not work the way it needs to work. Any suggestions?
Use the overload of String.Split() that takes an array of strings instead of the overload that takes an array of chars.
string[] result = text.Split(new string[] { " ", Environment.NewLine },
StringSplitOptions.None);
string fill = #"one two three four";
string[] result = fill.Split(new string[] { " ", Environment.NewLine },
StringSplitOptions.None);
foreach (var s in result)
{
Console.WriteLine(s);
}
Here is a DEMO.
But remember, Environment.NewLine is
A string containing "\r\n" for non-Unix platforms, or a string
containing "\n" for Unix platforms.
There is an overload that accepts strings:
string[] input = fill.Split(
new string[] { " ", Environment.NewLine },
StringSplitOptions.None);
You can also use Environment.NewLine instead of "\r\n".
But if you want to support all kinds of line-endings, you better specify all the popular possiblities:
string[] input = fill.Split(
new string[] { " ", "\n", "\r\n" },
StringSplitOptions.None);
I am testing to cut the strings via C#, but I am not getting the results correctly.
It is still showing the full text exactString.
String exactString = ABC##^^##DEF
char[] Delimiter = { '#', '#', '^', '^', '#', '#' };
string getText1 = exactString.TrimEnd(Delimiter);
string getText2 = exactString.TrimStart(Delimiter);
MessageBox.Show(getText1);
MessageBox.Show(getText2);
OUTPUT:
ABC##^^##DEF for both getText1 and getText2.
Correct OUTPUT should be
ABC for getText1 and DEF for getText2.
How do I fix it?
Thanks.
You want to split your string, not trim it. Thus, the correct method to use is String.Split:
String exactString = "ABC##^^##DEF";
var result = exactString.Split(new string[] {"##^^##"}, StringSplitOptions.None);
Console.WriteLine(result[0]); // outputs ABC
Console.WriteLine(result[1]); // outputs DEF
You are looking for String.Replace, not Trim.
char[] Delimiter = { '#', '^' };
string getText1 = exactString.Replace(Delimiter,'');
Trim only removes the characters at the beginning, Replace looks through the whole string.
You can split strings up in 2 pieces using the (conveniently named) String.Split method.
char[] Delimiter = { '#', '^' };
string[] text = exactString.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries);
//text[0] = "ABC", text[1] = "DEF
you can use String.Split Method
String exactString = "ABC##^^##DEF";
string[] splits = exactString.Split(new string[]{"##^^##"}, StringSplitOptions.None);
string getText1 = splits[0];
string getText2 = splits[1];
MessageBox.Show(getText1);
MessageBox.Show(getText2);
Basically i want to tokenise each word of the paragraph and then perform stopword removal. Which will be preprocessed data for my algorithm.
You can remove all punctuation and split the string for whitespace.
string s = "This is, a sentence.";
s = s.Replace(",","").Replace(".");
string words[] = s.split(" ");
if read from text file or any text you can:
char[] dele = { ' ', ',', '.', '\t', ';', '#', '!' };
List<string> allLinesText = File.ReadAllText(text file).Split(dele).ToList();
then you can convert stop-words to dictionary and save your document to list then
foreach (KeyValuePair<string, string> word in StopWords)
{
if (list.contain(word.key))
list.RemovAll(s=>s==word.key);
}
You can store all separation symbols and stopwords in constants or db:
public static readonly char[] WordsSeparators = {
' ', '\t', '\n', '\n', '\r', '\u0085'
};
public static readonly string[] StopWords = {
"stop", "word", "is", "here"
};
Remove all puctuations. Split text and filter:
var words = new List<string>();
var stopWords = new HashSet<string>(TextOperationConstants.StopWords);
foreach (var term in text.Split(TextOperationConstants.WordsSeparators))
{
if (String.IsNullOrWhiteSpace(term)) continue;
if (stopWords.Contains(term)) continue;
words .Add(term);
}