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();
~~~
Related
I am trying to write "text" into a file with
private void WriteToLogs(string text)
{
File.AppendAllText(todayMessageLog, $"({DateTime.Now}) Server Page: \"{text.Trim()}\"\n");
}
The text comes out as this:
"text (a bunch of white space)"
The text string is made up of these:
string username = e.NewClientUsername.Trim().Replace(" ", "");
string ip = e.NewClientIP.Trim().Replace(" ", "");
WriteToLogs($"{username.Trim().Replace(" ", "")} ({ip.Trim().Replace(" ", "")}) connected"); // NONE OF THESE WORKED FOR REMOVING THE WHITE SPACE
The "e" parameter comes from a custom EventArgs class in another namespace and NewClientIP and NewClientUsername are properties inside the class
As you can see, I tried with both Trim and Replace on both the strings themselves and the method but nothing removes the white space.
If the Trim() and Replace() methods do not work, the string is likely not padded with the usual white-space characters like SPACE or TAB, but something else. There are many other characters which can show up blank.
Try printing the result with something like BitConverter.ToString(Text.Encoding.UTF8.GetBytes(text)). Spaces would show up as 20-20-20-..., but you will probably get something else.
The white space shows up as 00, not 20, how can I remove it?
Good. Use the argument to the Trim() method, like so:
var text = "Blah\0\0\0\0";
text.Length → 8
text.Trim('\0').Length → 4
I hope that this working for you
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
//This is your text
string input = "This is text with far too much "+
"This is text with far too much "+
"This is text with far too much ";
//This is the Regex
string pattern = "\\s";
//Value with the replace
string replacement = "";
//Replace
string result = Regex.Replace(input, pattern, replacement);
//Result
Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);
}
}
If you want to trim white spaces (not only ' ', but \t, \U00A0 etc.) as well as \0 (which is not white space), you can try regular expressions:
using System.Text.RegularExpressions;
...
// Trim whitespaces and \0
string result = Regex.Replace(input, #"(^[\s\0]+)|([\s\0]+$)", "");
For reference
// Trim start whitespaces and \0
string resultStart = Regex.Replace(input, #"^[\s\0]+", "");
// Trim end whitespaces and \0
string resultEnd = Regex.Replace(input, #"[\s\0]+$", "");
Same idea (regular expressions), but different pattern if you want not to trim but remove white spaces and \0:
string result = Regex.Replace(input, #"[\s\0]+", "");
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);
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("\" \" ", " ");
}
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