I have a lot of strings that look like this:
current affairs
and i want to make the string be :
current affairs
i try to use Trim() but it won't do the job
Regex can do the job
string_text = Regex.Replace(string_text, #"\s+", " ");
You can use regular expressions for this, see Regex.Replace:
var normalizedString = Regex.Replace(myString, " +", " ");
If you want all types of whitespace, use #"\s+" instead of " +" which just deals with spaces.
var normalizedString = Regex.Replace(myString, #"\s+", " ");
Use a regular expression.
yourString= Regex.Replace(yourString, #"\s+", " ");
You can use a regex:
public string RemoveMultipleSpaces(string s)
{
return Regex.Replace(value, #"\s+", " ");
}
After:
string s = "current affairs ";
s = RemoveMultipleSpaces(s);
Using Regex here is the way,
System.Text.RegularExpressions.Regex.Replace(input, #”\s+”, ” “);
This will removes all whitespace characters including tabs, newlines etc.
First you need to split the whole string and then apply trim to each item.
string [] words = text.Split(' ');
text="";
forearch(string s in words){
text+=s.Trim();
}
//text should be ok at this time
Related
I have source string
string source = "hemant \\\n test new line \\\t test tab";
want string using regular expression
string destination = "hemant test new line test tab"
(Here i just replace \n and \t with " " i.e white space)
So i had try
string destination = Regex.Replace(source, "[\\\n\\\\t]", " ");
this gives me destination = hema test ew line es ab "
Here it remove the \\n and \\t. But remove the nt from hemant n from new
string destination = Regex.Replace(source, "[\\n\\t]", " ");
this don't do anything
try this:
string destination = Regex.Replace(source, #"(?:\\n)|(?:\\t)", " ");
or even more simple:
string destination = Regex.Replace(source, #"\\[nt]", " ");
"[\\\n\\\\t]" is a class that describes \ n and t as individual characters so you lose all n & t characters.
Use: "\\\\\\n|\\\\\\t"
You can just use string.Replace for that.
destination = source.Replace("\\\n", "").Replace("\\\t", "");
But note that will leave two spaces between "hemat" and "test", as well as between "line" and "test". If you want to compress the spaces as well you can use this regular expression.
var destination = Regex.Replace(source, #"\s*(\\\n|\\\t)\s*", " ");
Notice the use of # so you don't have to double up on the backslashes.
Or assuming your string is actually
source = "hemant \\n test new line \\t test tab";
That is a backslash character followed by the letter "n" and a backslash followed by the letter "t". Then you could do something like this.
var destination = Regex.Replace(source, #"\s*\\[nt]\s*", " ");
Just use source.Replace("\\", " ").Replace("\n", " ").Replace("\t", " "); no need for regex here.
How can I remove empty space between " " ?.
I have more than 100 lines of rich text box and my sentence goes like below with empty space between " ".
my sentence
remove only " railing" spaces of a string in Java
remove only " trailing" spaces of a string in Java
remove only " ling" spaces of a string in Java
remove only " ing" spaces of a string in Java
.
.
.
should be:
remove only "railing" spaces of a string in Java
remove only "trailing" spaces of a string in Java
remove only "ling" spaces of a string in Java
remove only "ing" spaces of a string in Java
.
.
.
My code
richTextBox1.lines.Trim().Replace("\" \" ", " ");
Using regex:
string RemoveBetween(string s, char begin, char end)
{
Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", begin, end));
return regex.Replace(s, string.Empty);
}
string s = "remove only \"railing\" spaces of a string in Java";
s = RemoveBetween(s, '"', '"');
source: https://stackoverflow.com/a/1359521/1714342
You can define between which characters you wish to remove string. Read more about Regex.Replace
EDIT:
missunderstood, you are just missing assign in richTextBox1.lines.Trim().Replace("\" \" ", " ");
make it:
richTextBox1.lines = richTextBox1.lines.Trim().Replace("\" \" ", " ");
Replace is not changing string.
Try this:
richTextBox1 = richTextBox1.lines.Trim().Replace(" \" ", " \"");
You're missing the reassign to richTextBox1. Replace() returns string value with correct text.
Your code should be:
for(int i = 0; i < richTextBox1.Lines.Count(); i++)
{
richTextBox1.Lines[i] = richTextBox1.Lines[i].Trim().Replace("\" \" ", " ");
}
Is there a regex pattern that can remove .zip.ytu from the string below?
werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222
Here is an answer using regex as the OP asked.
To use regex, put the replacment text in a match ( ) and then replace that match with nothing string.Empty:
string text = #"werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222";
string pattern = #"(\.zip\.ytu)";
Console.WriteLine( Regex.Replace(text, pattern, string.Empty ));
// Outputs
// werfds_tyer.abc_20111223170226_20111222.20111222
Just use String.Replace()
String.Replace(".zip.ytu", "");
You don't need regex for exact matches.
txt = txt.Replace(".zip.ytu", "");
Why don't you simply do above?
Don't really know what is the ".zip.ytu", but if you don't need exact matches, you might use something like that:
string txt = "werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222";
Regex mRegex = new Regex(#"^([^.]*\.[^.]*)\.[^.]*\.[^_]*(_.*)$");
Match mMatch = mRegex.Match(txt);
string new_txt = mRegex.Replace(txt, mMatch.Groups[1].ToString() + mMatch.Groups[2].ToString());
use string.Replace:
txt = txt.Replace(".zip.ytu", "");
Here is the method I use for more complex repaces. Check out the link: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(v=vs.110).aspx for a Regular Expression replace. I added the code below as well.
string input = "This is text with far too much " +
"whitespace.";
string pattern = "\\s+";
string replacement = " ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);
I'm using the code below to determine if the value entered into textBoxAddress.Text is a number (fax number) or an email address. My isNumber test below won't work if the fax number has spaces so I'm trying to remove all white space with the Regex, the whitespace isn't being removed. Can you spot my problem?
Thank you.
string faxOrEmail = textBoxAddress.Text;
faxOrEmail = Regex.Replace(faxOrEmail, #"\s+", " "); //<---Regex
bool testForFax = IsNumber(faxOrEmail);
if (testForFax == true)
{
//send fax
MessageBox.Show(faxOrEmail);
}
else
{
backgroundWorker1.RunWorkerAsync(); //Send Email
}
static bool IsNumber(string value)
{
// Return true if this is a number.
int number1;
return int.TryParse(value, out number1);
}
You are replacing one or more spaces with a single space.
Regex.Replace(faxOrEmail, #"\s+", string.Empty);
This doesn't remove dashes, and your IsNumber method isn't necessary.
string.Join(string.Empty, faxOrEmail.ToCharArray().Where(x=> char.IsNumber(x)))
Try this:
faxOrEmail = Regex.Replace(faxOrEmail, #"\s+", "");
I don't know C# to well but if you want to remove spaces try replacing it with empty string not space :)
You are replacing the spaces, with spaces! Use String.Empty to avoid these problems:
faxOrEmail = Regex.Replace(faxOrEmail, #"\s+", String.Empty);
All of the above:
or
Don't use a regular expression at all. Just use the Replace method and replace " " with ""
If you want to replace 2 or more consecutive spaces with one space, then do this:
faxOrEmail = Regex.Replace(faxOrEmail, #"\s{2,}", " ");
If you want to remove all spaces, then do this:
faxOrEmail = Regex.Replace(faxOrEmail, #"\s+", string.Empty);
If faxOrEmail only contains spaces (" ") and no tabs for instance, then you can go with:
faxOrEmail = faxOrEmail.Replace(" ", "");
On the other hand, you could also remove everything (including dashes, slashes, parentheses, etc.) but decimal digits with Regex:
faxOrEmail = Regex.Replace(faxOrEmail, "[^0-9]", "");
I am trying to remove " from a string using Regex.
I am receiving a string into a method, I would like to take the string and split it up into the words that are in the string.
My Code is below, hopefully you can see what I am doing.
The problem I am having is trying to tell Regex that " is what I would like to remove. I have tried numerous ways: I have searched Google for a answer and have had to resort to here.
search_string looks like this: blah="blah" la="la" ta="ta" and in the end I want just the blah blah la la ta ta.
public blahblah blahblah(blah blah, string search_string)
{
Regex r = new Regex(#"/"+");
string s3 = r.Replace(search_string, #" ");
Regex r2 = new Regex(" ");
Regex r3 = new Regex("=");
string[] new_Split = { };
string[] split_String = r2.Split(s3);
foreach (string match in split_String)
{
new_Split = r3.Split(match);
}
//do blahblah stuff with new_Split[1] .. etc
// new_Split[0] should be blah and new_Split[1] should
// be blah with out "", not "blah"
return blah_Found;
Just use:
myString = myString.Replace( "\"", String.Empty );
[Update]
The String.Empty or "" is not a space char. You wrote this
blah="blah" la="la" ta="ta"
you want to convert to
blah blah la la ta ta
So you have white spaces anyway. If you want this:
blahblahlalatata
you need to remove them too:
myString = myString.Replace( "\"", String.Empty ).Replace( " ", String.Empty );
for '=' do it again, and so on...
You need to be more precise in your questions.
As a quick thought - and barking maybe up entirely the wrong tree, but wouldnt you want something like
Regex r = new Regex("(\".*\")");
eg, a reg expression of ".*"
This is one way to do it.
It will Search for anything in that form: SomeWord="somethingelse"
and replace it with SomeWord somethingelse
var regex = new Regex(#"(\w+)=\""(.+)\""");
var result = regex.Replace("bla=\"bla\"", "$1 $2");
I can't help you with Regex.
Anyway if you only need to remove = and " and split words you could try:
string[] arr = s
.Replace("="," ")
.Replace("\""," ")
.Split(new string[1] {" "}, StringSplitOptions.RemoveEmptyEntries);
I did it in 2 passes
string input = "blah=\"blah\" la=\"la\" ta=\"ta\"";
//replace " and = with a space
string output = Regex.Replace(input, "[\"=]", " ");
//condense the spaces
output = Regex.Replace(output, #"\s+", " ");
EDIT:
Treating " and = differently as per comment.
string input = "blah=\"blah\" la=\"la\" ta=\"ta\"";
//replace " and = with a space
string output = Regex.Replace(input, "\"", String.Empty);
output = Regex.Replace(output, "=", " ");
Clearly regex is a bit overkill here.