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("\" \" ", " ");
}
Related
I have some string. I need a regex that will replace each occurrence of symbol that is not space + '<' with the same symbol + space + '<'.
In other words if there is '<' without ' ' before it it must add the space.
I've tried something like :
string pattern = "[^ ]<";
string replacement = "$0" + "<";
string result = Regex.Replace(html, pattern, replacement);
Obviously not working as I want.
string pattern = "([^ ])<";
string replacement = "$1" + " <";
You can try something like this.
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.
I want to remove tabs from a string. I am using this code but its not working.
string strWithTabs = "here is a string with a tab";
// tab-character
char tab = '\u0009';
String line = strWithTabs.Replace(tab.ToString(), "");
I tried this, but still its not working
String line = strWithTabs.Replace("\t", "");
It worked with String line = strWithTabs.Replace(" ", "");
But is their any other way to Identify tabs ?
I also looked at this post Removal of Tab-whitespace?
But it removed all the spaces from the string, where as I just want to remove Tabs.
Tab and space are not same, if tab is converted into spaces, replacing just "\t" will not work.
Below code will find tab and replace with single space and also find multiple spaces and replace it with single space.
string strWithTabs = "here is a string with a tab and with spaces";
string line = strWithTabs.Replace("\t", " ");
while(line.IndexOf(" ") >= 0)
{
line = line.Replace(" ", " ");
}
Edit: Since this is accepted, I'll amend it with the better solution posted by Emilio.NT which is to use Regex instead of while:
string strWithTabs = "here is a string with a tab and with spaces";
const string reduceMultiSpace= #"[ ]{2,}";
var line= Regex.Replace(strWithTabs.Replace("\t"," "), reduceMultiSpace, " ");
Because " " is not equal to tab character. \t is. It is an escape sequence character.
For example;
string strWithTabs = "here is a string\twith a tab";
char tab = '\u0009';
String line = strWithTabs.Replace(tab.ToString(), "");
line will be here is a stringwith a tab
You can't say a sentence like \t is equal to 6 spaces for example.
Use Regular Expression to reduce multiple spaces to one:
var strWithTabs = "here is a string with a tab and spaces";
const string reduceMultiSpace= #"[ ]{2,}";
var line= Regex.Replace(strWithTabs.Replace("\t"," "), reduceMultiSpace, " ");
Seems to be the compactest ...
//Eliminate tabs & multiple spaces => only 1 space between words
string input1 = " Hello World !";
Regex rgx2 = new Regex("\t|\\s+");
string result = rgx2.Replace(input1, " ");
Console.WriteLine("Original String: {0}", input1);
Console.WriteLine("Replacement String: {0}", result);
Console.ReadKey();
~~~
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
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]", "");