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

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

Related

How to remove special characters from input text box and display in a label?

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
I have tried several ways to display the text but nothing has warranted the correct output
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.
Please try given below. Thanks.
string inputValue = textBox1.Text
.Trim()
.Replace(".", "")
.Replace(",", "")
.Replace(";", "");
If you have dynamic number of chars, just loop the replacement:
char[] delimChar = { ',', '.', ';' };
var labelBuilder = new StringBuilder(textBox1.Text);
foreach(var c in delimChar)
labelBuilder.Replace(c, string.Empty);
label1.Text = labelBuilder.ToString().Trim();
Or you may still use Regex:
var label = Regex.Replace(textBox1.Text, "[,.;]", string.Empty).Trim();
I'd also recommend to trim string after replacement to handle cases like PO 145 ;

Compare two strings in c# containing random URL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to compare the below strings. The problem I have is the url in both strings will be different every time e.g:
www.google.com
http://www.google.com
google.co.uk!
So contains cannot match the strings because of the URL not matching.
String1 = "This is my string http://www.google.co.uk and that was my url"
String2 = "this is my string google.gr and that was my url"
So I basically want to compare the contents of the string minus the URl, each string can contain different text each time so looking for the URL at the same location each time will not work.
I have searched extensively on here for an answer to this problem, but I was unable to find a working solution.
Thanks in advance
Use regular expressions to remove links:
String string1 = "This is my string http://www.google.co.uk and that was my url";
String string2 = "this is my string http://google.gr and that was";
Regex rxp = new Regex(#"http://[^\s]*");
String clean1 = rxp.Replace(string1, "");
String clean2 = rxp.Replace(string2, "");
And now you can compare clean1 with clean2. OFC regexp above is just an example it'll just remove url's staring with "http://". You may need something more sophisticated, based on your real data.
Using Regular Expressions:
Regex regex = new Regex(#"\s((?:\S+)\.(?:\S+))");
string string1 = "This is my string http://www.google.co.uk and that was my url.";
string string2 = "this is my string google.gr and that was my url.";
var string1WithoutURI = regex.Replace(string1, ""); // Output: "This is my string and that was my url."
var string2WithoutURI = regex.Replace(string2, ""); // Output: "this is my string and that was my url."
// Regex.Replace(string1, #"\s((?:\S+)\.(?:\S+))", ""); // This can be used too to avoid having to declare the regex.
if (string1WithoutURI == string2WithoutURI)
{
// Do what you want with the two strings
}
Explaining the regex \s((?:\S+)\.(?:\S+))
1. \s Will match any white space character
2. ((?:\S+)\.(?:\S+)) Will match the url until the next white space character
2.1. (?:\S+) Will match any non-white space character without capturing the group again (with the ?:)
2.2. \. Will match the character ".", because it will always exist in a url
2.3. (?:\S+)) Again, will match any non-white space character without capturing the group again (with the ?:) to get everything after the dot.
That should do the trick...

C# - Count a specific word in richTextBox1 and send the result to label1 [duplicate]

This question already has answers here:
How would you count occurrences of a string (actually a char) within a string?
(34 answers)
Closed 7 years ago.
I'm not sure, if this question is unique, but I couldn't find the answer that I was looking for.
I simply need a C# code that counts how many times a word appear in richTextBox1 and send the result to label1.
Example;
label1.text = how many times the word "house" appears in richTextBox1.
I know that I should try first but believe me I tried and I failed. I am new to this so I hope someone can show me.
Regards
I would solve it using Regular Expressions as follows:
using System.Text.RegularExpressions;
...
string searchstring = ....;
string input = richTextBox1.Text;
int count = Regex.Matches(input, searchstring, RegexOptions.IgnoreCase).Count;
label1.Text = count.ToString();
Do please note, that the code above only works for one single word containing the characters a-z, A-Z, 0-9 and _.
EDIT No.1: If you want to match any exact sequence, you could use the following method:
using System.Text.RegularExpressions;
...
string searchstring = ....;
string input = richTextBox1.Text;
searchstring = Regex.Escape(searchstring);
int count = Regex.Matches(input, searchstring, RegexOptions.IgnoreCase).Count;
label1.Text = count.ToString();
The method above should match any UTF-16 character sequence and count it accordingly.

How to skip `\r \n ` in string

I am working on a simple converter which converts a text to another language,
suppose i have two textboxes and in 1st box you enter the word Index and press the convert button.
I will replace your text with this فہرست an alternative of Index in urdu language but i have a problem if you enter word index and gives some spaces or gives some returns then i get text of that textbox in c# like this Index \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n now how can i get rid of this i want to get simple Index always .
Thanks for answer and please feel free to comment if you have any question
Try using the Trim method if the new lines are only at the end of beginning:
input = input.Trim();
You can use Replace, if you want to remove new lines anywhere in the string:
// Replace line break with spaces
input = input.Replace("\r\n", " ");
// (Optionally) Combine consecutive spaces to one space (probalby not most efficient but should work)
while (input.Contains(" ")) { input = input.Replace(" ", " "); }
If you want to prevent newlines completely, most TextBox Controls have a property like MultiLine or similar, that, when set, prevents entering more than one line.
input.Replace(Environment.NewLine, string.Empty).Replace(" ", string.Empty);
User Replace to remove characters from the 'inside' of the string. Trim removes characters only at the begining and end of string.
This should suffice to remove whitespaces as defined by Char.IsWhiteSpace (blanks, newlines etc)
string wordToTranslate = textBox1.Text.Trim();
however, if your textbox contains multiple words then you should use a different approach
string[] words = textBox1.Text.Split((char[]) null, StringSplitOptions.RemoveEmptyEntries);
foreach(string wordToTranslate in words)
ExecTranslation(wordToTranslate);
using Split with char[] null as separator allows to identify every whitespaces as valid word separator
Add all chars you want to ignore to the string:
var cleanChars = text.Where(c => !"\n\r".Contains(c));
string cleanText = new string(cleanChars.ToArray());
That works because string implements IEnumerable<char>.

C# RegEx to create string array split on spaces and phrases (in quotes) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular Expression to split on spaces unless in quotes
I am dealing with various strings that I need to split into an array wherever there is a space, except for if that space exists within "quotes".
So for example, I would like this:
this is "a simple" test
..to become:
[0] = this
[1] = is
[2] = "a simple"
[3] = test
Note I would like to retain the quotes surrounding the phrase, not remove them.
The regex:
".*?"|[^\s]+
Usage:
String input = #"this is ""a simple"" test";
String[] matches =
Regex.Matches(input, #""".*?""|[^\s]+").Cast<Match>().Select(m => m.Value).ToArray();

Categories