How to sanitize a string removing specific character? [duplicate] - c#

This question already has answers here:
Is there a simple way to remove multiple spaces in a string?
(26 answers)
How do I replace multiple spaces with a single space in C#?
(28 answers)
Closed 4 years ago.
I have this string:
PO Box 162, Ferny Hills
QLD 4055
Brisbane
which contains these character:
I want remove this charactes, so I tried:
info.Address = dd[i].InnerText
.Replace("\n", " ")
.Replace(" ", "")
.Replace(",", ", ");
but didn't works, I get all the character of the string attached. I'm expecting this result: PO Box 162, Ferny Hills QLD 4055 Brisbane.

Well, you replaced all blanks by nothing ( .Replace(" ", "") ). What did you expect? Now all your blanks are gone.
If you don't want that, don't do it.

you can try like this, by splitting and trimming the string to remove spaces and join them back by newline
var address = dd[i].InnerText.Split(new[] { Environment.NewLine })
.Select(s => s.Trim());
info.Address = String.Join(Environment.NewLine, address);

Related

How to eliminate delimiters and spaces from user inputed text? [duplicate]

This question already has answers here:
Remove characters from C# string
(22 answers)
Closed 3 years ago.
create a project that will allow the user to enter a mailing address. The address may not contain any leading or trailing spacebars and may not contain the following special characters.
• Period key (.)
• Comma (,)
• Semicolon (;)
The user will enter a mailing address into a Text Box. When the appropriate button is selected, the entered address will be checked for any invalid characters. The resulting/corrected address will then be displayed into a Label.
I have written code but I cannot figure out how to display it in the label box without the period, comma, and semicolon
string wordString;
char[] delimChar = { ',', '.', ';' };
wordString = entryTextBox.Text;
wordString = wordString.Trim();
string[] delimString = wordString.Split(delimChar);
I have no error messages in the code but need some additional help.
You could just use regex replace
var input = " P.O, Box123;";
var pattern = #"^ *|\.|,|;";
var result = Regex.Replace(input, pattern, "");
Console.WriteLine(result);
Output
PO Box123
Demo Here

Delete empty spaces from the beginning and Ending of the textbox c# [duplicate]

This question already has answers here:
C# Trim string regardless of characters
(4 answers)
Closed 5 years ago.
How to remove all white spaces from the right and left end of a string, like so:
"hello" return "hello"
"hello " return "hello"
" hello " return "hello"
" hello world " return "hello world"
textbox1.Text when click button
Your textbox1.Text returns a string. As with any string all you need to do is textbox1.Text.Trim(), this will remove any leading or trailing whitespace

Removing White spaces from string is not working in c# [duplicate]

This question already has answers here:
How do I replace multiple spaces with a single space in C#?
(28 answers)
Closed 6 years ago.
I am trying to remove whitespaces from string but it is not working
string status = " 18820 Pacific Coast Highway
Malibu, CA 90265";
string status1 = status.Trim();
Console.Write(status1);
The above code is not working
Expected Output:
18820 Pacific Coast Highway Malibu, CA 90265
Trim removes leading and trailing symbols (spaces by default). Use Regular expression instead.
RegEx.Replace(status, "\s+", " ").Trim();
Trim() only works at the start and end of a string. This should work:
string status1 = Regex.Replace(status,#"\s+"," ").Trim();
string status = " 18820 Pacific Coast Highway
Malibu, CA 90265";
string status1 = status.Trim();
Console.Write(status1);
status = status .Replace(" ", "");
But the above code will remove all the whitespaces.
If you want to have whitespace at the end of everyword, then use foreach as mentioned in this link
How to trim whitespace between characters

Extract part of the string [duplicate]

This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 7 years ago.
I have thousands of lines of string type data, and what I need is to extract the string after AS. For example this line:
CASE END AS NoHearing,
What I want would be NoHearing,
This line:
CASE 19083812 END AS NoRequset
What I need would be NoRequset
So far I have tried couple ways of doing it but no success. Can't use .split because AS is not Char type.
If this will be the only way that AS appears in the string:
noRequestString = myString.Substring(myString.IndexOf("AS") + 3);
Using Regex I extract all between the AS and a comma:
string data = #"
CASE END AS NoHearing,
CASE 19083812 END AS NoRequset
";
var items = Regex.Matches(data, #"(?:AS\s+)(?<AsWhat>[^\s,]+)")
.OfType<Match>()
.Select (mt => mt.Groups["AsWhat"])
.ToList();
Console.WriteLine (string.Join(" ", items)); // NoHearing NoRequset

Split a Pascal-case string into logical set of words [duplicate]

This question already has answers here:
Closed 14 years ago.
I would like to take a pascal-cased string like "CountOfWidgets" and convert it into something more user-friendly like "Count of Widgets" in C#. Multiple adjacent uppercase characters should be left intact. What is the most efficient way to do this?
NOTE: Duplicate of .NET - How can you split a "caps" delimited string into an array?
Don't know about efficient but at least it's terse:
Regex r = new Regex("([A-Z]+[a-z]+)");
string result = r.Replace("CountOfWidgets", m => (m.Value.Length > 3 ? m.Value : m.Value.ToLower()) + " ");

Categories