Splitting strings with multiple characters [duplicate] - c#

This question already has answers here:
Regex.Split() on comma, space or semi-colon delimitted string
(5 answers)
Closed 6 years ago.
if (clientInfo.cf.geo_region != null)
{
List<string> geoListRegion = clientInfo.cf.geo_region.Split(new string[] { ",", ", " }, StringSplitOptions.RemoveEmptyEntries).ToList();
rs_product_hit = rs_product_hit.Where(ph => geoListRegion.Contains(ph.region));
}
I want to be able to input either "AMAC,South America" or "AMAC, South America" and split where the comma is placed with/without a space after. Unfortunately, my output is only splitting the comma without the space, giving me the correct output if the input is "AMAC,South America". What could I do to get the same result with or without the space after the comma?

Simple Workaround: changing the order of your splitting strings to new string[] { ", ", ","} will resolve the issue. You could also use string.Trim() to remove leading or trailing spaces.

You're close, but you need to do Trim()
if (clientInfo.cf.geo_region != null)
{
List<string> geoListRegion = clientInfo.cf.geo_region.Split(new string[] { ",", ", " }, StringSplitOptions.RemoveEmptyEntries).ToList();
rs_product_hit = rs_product_hit.Where(ph => geoListRegion.Contains(ph.region.Trim()));
}

Two ways:
swap the order of your splitting strings as answered by Bastian Thiede.
Regex. Remove all whitespace after commas using Regex.Replace(myString, #",\s+", ""). The benefit of using this method is that it removes any number of spaces succeeding a comma.

Related

Trim is not returning a string without the characters I want to remove [duplicate]

This question already has answers here:
C# Trim() vs replace()
(8 answers)
Closed 3 years ago.
I want to remove 2 chars from a string: '-', '.'
According to microsoft documentation, if I provide a array of characters as a parameter to trim, it should remove then and return a new string.
Here is the code I trying:
char[] charsToTrim = { '-', '.'};
string newCPF = usuario.Cpf.Trim(charsToTrim);
usuario.Cpf = newCPF;
_context.Add(usuario);
The string is something like this 000.000.000-00, but trim is not removing the . and -
What I'm doing wrong?
Trim only removes the specified characters from the start and the end of the string.
You could just use String.Replace to remove those characters:
string newCPF = usuario.Cpf.Replace("-", "").Replace(".", "");

Splitting string with dots and spaces [duplicate]

This question already has answers here:
Regex split string but keep separators
(2 answers)
Closed 7 years ago.
Let's say I have the following string: "This is a test. Haha.". I want to split it so that it becomes these there lines:
Hey.
This is a test.
Haha.
(Note that the space after the dot is preserved).
I tried to split the string using the Split method, and split by the dot, but it returns 3 new strings with the space before the string, and it removes the dots. I want to keep the space after the dot and keep the space.
How can I achieve this?
EDIT: I found a workaround, but I'm sure there's a simpler way:
string a = "Hey. This is a test. Haha.";
string[] splitted = a.Split('.');
foreach(string b in splitted)
{
if (b.Length < 3)
{
continue;
}
string f = b.Remove(0, 1);
Console.WriteLine(f + ". ");
}
I can't test this but due to the post of Darin Dimitrov :
string input = "Hey. This is a test. Haha.";
string result = input.Replace(". ", ".\n");

Remove characters between different parameters [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 7 years ago.
I have a string of different emails
ex: "email1#uy.com, email2#iu.it, email3#uu.edu" etc, etc
I would like to formulate a Regex that creates the following output
ex: "email1,email2,email3" etc, etc
How can I remove characters between an "#" and "," but leaving a "," and a Space in C#
Thank you so much for the help!!
If you want to replace all characters between # and comma by blank, the easiest option is to use Regex.Replace:
var emails = "a#m.com, b#m.com, d#m.com";
var result = Regex.Replace(emails, "#[^,]+", string.Empty);
// result is "a, b, d"
Please note that it leaves spaces after comma in the result, as you wanted in your question, though your example result has spaces removed.
The regular expression looks for all substrings starting '#' characters, followed by any character which is not comma. Those substrings are replaced with empty string.
Replacing all occurrences of #[^,]+ with an empty string will do the job.
The expression matches sequences that start in #, inclusive, up to a comma or to the end, exclusive. Therefore, commas in the original string of e-mails would be kept.
Demo.
Maybe you don't need to use a regex, in that case you can do the following:
string input = "email1#uy.com, email2#iu.it, email3#uu.edu";
input = input.Replace(" ", "");
string[] ocurrences = input.Split(',');
for (int i = 0; i < ocurrences.Length; i++)
{
string s = ocurrences[i];
ocurrences[i] = s.Substring(0, s.IndexOf('#'));
}
string final = string.Join(", ", occurences);

How to remove spaces and newlines in a string [duplicate]

This question already has answers here:
Fastest way to remove white spaces in string
(13 answers)
Closed 9 years ago.
sorry if they are not very practical for C # Asp.Net, I hope to make me understand
I have this situation
string content = ClearHTMLTags(HttpUtility.HtmlDecode(e.Body));
content=content.Replace("\r\n", "");
content=content.Trim();
((Post)sender).Description = content + "...";
I would make sure that the string does not contain content nor spaces (Trim) and neither carriage return with line feed, I'm using the above code inserted but it does not work great either
any suggestions??
Thank you very much
Fabry
You can remove all whitespaces with this regex
content = Regex.Replace(content, #"\s+", string.Empty);
what are whitespace characters from MSDN.
Btw you are mistaking Trim with removing spaces, in fact it's only removing spaces at the begining and at the end of string. If you want to replace all spaces and carige returns use my regex.
this should do it
String text = #"hdjhsjhsdcj/sjksdc\t\r\n asdf";
string[] charactersToReplace = new string[] { #"\t", #"\n", #"\r", " " };
foreach (string s in charactersToReplace)
{
text = text.Replace(s, "");
}
simple change only you missed # symbol
string content = ClearHTMLTags(HttpUtility.HtmlDecode(e.Body));
content=content.Replace(#"\r\n", "");
content=content.Trim();
((Post)sender).Description = content + "...";

How do i remove all white spaces from a string? [duplicate]

This question already has answers here:
Efficient way to remove ALL whitespace from String?
(18 answers)
Closed 8 years ago.
I have this code:
if (TextParsingConfiguration.join == true)
{
images = images.Trim(' ');
}
if (TextParsingConfiguration.removecommas == true)
{
images = ReplacingChars(images, new[] { ',', '"' }, " ");
}
join and removecommas are bool variables im using them also in form1 in checkBoxes.
And the ReplacingChars method:
public static string ReplacingChars(string source, char[] toReplace, string withThis)
{
return string.Join(withThis, source.Split(toReplace, StringSplitOptions.None));
}
Maybe im wrong here with the logic but i wanted to give the user two options.
to remove all Remove commas and Quotation marks this the removecommas bool variable.
to join meaning to remove all the whit spaces but not the commas and Quotation marks just remove/delete the white spaces so the whole text will be like a block of text.
The question if number 2 is logic at all ? And if not what other options to remove(clean) i can make ?
The removecommas is working. Its removing commas and Quotation marks and leave the spaces as it was.
Try this :
images= images.Replace(" ", String.Empty);
You could use Regex.Replace like this:
string newString = Regex.Replace(sourceString, #"\s+", replacement);
Maybe something like this:
images = Regex.Replace(images, #"\s+", "");
You can split the text like below,
string[] sp = new string[] { " ", "\t", "\r" };
string[] aa = images.Split(sp, StringSplitOptions.RemoveEmptyEntries);

Categories

Resources