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);
Related
I have below string as input
string input = "{A.0a,100,0002_VL}{=}{A.0a,0400,0001_VL}{+}{A.0a,0410,0002_VL}{+}{A.0a,0420,0003_VL}"
I want below output as string array
string[] output
output[0] = "A.0a,100,0002_VL"
output[1] = "="
output[2] = "A.0a,0400,0001_VL"
output[3] = "+"
output[4] = "A.0a,0410,0002_VL"
output[5] = "+"
output[6] = "A.0a,0420,0003_VL"
I want to use RegEx.Split function but unable to identity a pattern. Is it possible to use? Kindly help me.
char[] charArray = { '{', '}' };
var inputArray = input.Split( charArray, StringSplitOptions.RemoveEmptyEntries);
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,
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);
}
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();
I want to split a string into an array. The string is as follows:
:hello:mr.zoghal:
I would like to split it as follows:
hello mr.zoghal
I tried ...
string[] split = string.Split(new Char[] {':'});
and now I want to have:
string something = hello ;
string something1 = mr.zoghal;
How can I accomplish this?
String myString = ":hello:mr.zoghal:";
string[] split = myString.Split(':');
string newString = string.Empty;
foreach(String s in split) {
newString += "something = " + s + "; ";
}
Your output would be:
something = hello; something = mr.zoghal;
For your original request:
string myString = ":hello:mr.zoghal:";
string[] split = myString.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
var somethings = split.Select(s => String.Format("something = {0};", s));
Console.WriteLine(String.Join("\n", somethings.ToArray()));
This will produce
something = hello;
something = mr.zoghal;
in accordance to your request.
Also, the line
string[] split = string.Split(new Char[] {':'});
is not legal C#. String.Split is an instance-level method whereas your current code is either trying to invoke Split on an instance named string (not legal as "string" is a reserved keyword) or is trying to invoke a static method named Split on the class String (there is no such method).
Edit: It isn't exactly clear what you are asking. But I think that this will give you what you want:
string myString = ":hello:mr.zoghal:";
string[] split = myString.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
string something = split[0];
string something1 = split[1];
Now you will have
something == "hello"
and
something1 == "mr.zoghal"
both evaluate as true. Is this what you are looking for?
It is much easier than that. There is already an option.
string mystring = ":hello:mr.zoghal:";
string[] split = mystring.Split(new char[] {':'}, StringSplitOptions.RemoveEmptyEntries);