How to replace " " " with space - c#

I am trying to replace double quotes with space but it's not working, says syntax error,
"'asd"asd', 'asdasda"sdsa'" // This is the string pattern I am getting "somehow"
string b = a.Replace(""", " "));

You have to escape the " with a backslash:
string b = a.Replace("\"", " ");
where string a is bla bla " bla " bla bla

You need to escape the qoute. Given you have had quotes, this is what will happen
string a = "\"asdasd\", \"asdasdasdsa\"";
string b = a.Replace("\"", " "); // b = " asdasd , asdasdasdsa "

Firstly,
I think you are declaring the string a in a wrong way.
Normally in a declaration of a string, there should be a double quote in each end, like string a = "...", and they won't be counted in the content of the string. If you want to include a double quote in the string, you should put a slash ahead of it, like: string a = "I said, \"Thanks.\"".
If you print a, you will get
I said, "Thanks."
Secondly, the same problem.
string b = a.Replace("\"", " "));

Related

How can I ensure a string has no more than a single whitespace anywhere in it?

I am taking a string as an input and due to some processing later on it is critical that the string does not contain 2 or more consecutive whitepaces anywhere in the string.
For example
string foo = "I am OK" is a valid string
string foo = " I am OK " is a valid string
string foo = " I am OK" is NOT a valid string due to the initial double white space
string foo = "I am OK " is NOT a valid string due to the trailing double whitespaces
string foo = " I am OK " is NOT a valid string since it has 2 whitespaces between am and OK
I think you get the picture, I tried to normalize the string using the following code
string normalizedQuery = apiInputObject.Query.Replace(" ", "");
But this only works I am sure the string has single whitespaces in it, thats why I need to ensure the string never has more so I can use that replace.
How can I make sure the string fits my format?
You may try using the regex pattern #"^(?!.*[ ]{2}).*$":
Match result = Regex.Match("One space", #"^(?!.*[ ]{2}).*$");
if (result.Success) {
Console.WriteLine("ONLY ONE SPACE OR LESS");
}
Use IndexOf.
public static void Main()
{
var tests = new string[]
{
"I am OK",
" I am OK ",
" I am OK",
"I am OK ",
" I am OK "
};
foreach (var test in tests)
{
var hasDoubleSpace = (test.IndexOf(" ") != -1);
Console.WriteLine("'{0}' {1} double spaces", test, hasDoubleSpace ? "has" : "does not have");
}
}
Output:
'I am OK' does not have double spaces
' I am OK ' does not have double spaces
' I am OK' has double spaces
'I am OK ' has double spaces
' I am OK ' has double spaces
Fiddle
Based on your requirement, I think you just need to replace two consecutive white-spaces to one.
foo=foo.Replace(" "," ");
As pointed by Gian, "Repeat until no double space is in the string".
You can use String.Contains to look for " ", but if the user can type ANYTHING, you have a bigger problem than simple spaces, like various unicode spaces, hieroglyphs et cetera (that is, unless your post-processing only looks for simple spaces and is otherwise unicode-tolerant).
I would loop through the entire string, character by character and check if two side by side characters are whitespace:
for(int i=0; i<foo.Length-1; i++){
if(foo[i] == ' ' && foo[i+1] == ' '){
return false;
}
}
return true;

Assigning many double quotes to string

I have a string with many double quotes like so:
string x = " I "" Am" bla"" bla" ble" ";
I know that I can escape each individual double quote and that it will not return an error but:
I am copying very large strings with different text each time;
I cannot afford to individually escape thousands of characters;
Using the character # before the first double quote does not work.
Is there any other way to automate the process apart from downloading the strings to a text file on the computer and importing it with File.ReadAllText?
Thank you in advance.
if you asking how to write characters like : "
string x = " I \"\" Am\" bla\"\" bla\" ble\" ";
and we use # before the first double quote to return new line like this
string x = #" bla bla bla bla
bla bla bla";

Display "" in a string output

I want to display sth like
string test1 = "test2";
so i would like this to be displayed in a richtextbox, but i don't know how to add " in a string output, i have everything working except the displaying of ""; and its C# :) so it should look like that:
string test2 = "test2";
richTextBox.Text = "string test1 = " + (the ") + test2 + (the ") + ";";
You can use backslashes (\) to "escape" a quotation mark. So this string is actually ":
string s = "\"";
Got it? If you still don't, just remember that when ever you want to write a quotation in a string, write \".
So your text would be something like this
richTextBox.Text = "string test1 = \"test2\";";
See? The outer most two quotes denote a string literal and the inner two quotes actually denote the actual quotes in the string. Also, don't forget the semicolon at the end. (You will be likely to forget this because you saw the semi colon in the string literal. But remember! The semicolon is in the string, it's "fake"!)
You have to escape the character " in your string. In C# in many other languages the escape key is \
So your final string will be :
richTextBox.Text = "string test1 = \"" + test2 + "\";";
You can do this by escaping the " like so \":
richTextBox.Text = "string test1 = \"test2\";"
Which will display:
string test1 = "test2";

want regular expression for \\n and \\t

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.

string trim remove space?

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("\" \" ", " ");
}

Categories