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.
Related
I have strings that look like this:
"\n 間違う\n "
It's really a \n and some spaces.
How can I remove these from the string?
Use Trim() which will remove any white-space (including new-lines, tabs, etc) from both left and right ends of a string:
var s = "\n 間違う\n ";
var trimmed = s.Trim();
// output
// 間違う
If you want to remove line-breaks between text (like 間\n違う\n) then you can use Replace:
var s = "間\n違う\n";
var trimmed = s.Trim().Replace(Environment.NewLine, string.Empty);
// output
// 間違う
Use String.Trim.
var s1 = "\n 間違う\n ";
var s2 = s1.Trim();
Debug.WriteLine(s2);
How would I replace periods with spaces, but preserve ...?
string test = "This.is.a.test...";
test = test.Replace(".", " ");
http://rextester.com/DLEHI1253
You could use this (?<!\.)\.(?!\.)
var regex = new Regex(#"(?<!\.)\.(?!\.)");
var ressult = regex.Replace("This.is.a.test..."," ");
Console.WriteLine(ressult);
Output
This is a test...
Demo 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();
~~~
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("\" \" ", " ");
}
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