I currently have a large text file and I am trying to replace unknown text between anything inside "" with #. I've tried to use:
string text = File.ReadAllText(#"c:\Users\Zero\Documents\test.txt");
string replacement = "#";
int start = text.IndexOf('"') + 1;
text = text.Replace(text.Substring(start, text.LastIndexOf('"') - start), replacement);
File.WriteAllText(#"c:\Users\Zero\Documents\test.txt", text);
Currently it's replacing all text in the file with one line. in other words, it's turning:
Hi how are you "test"
This is a test "123" test
"test" "test"
into
"#"
I need it to do this
Hi how are you "#"
This is a test "#" test
"#" "#"
A regular expression like this would come in handy:
"[^"]*"
For example:
text = Regex.Replace(text, "\"[^\"]*\"", "\"#\"");
Related
I'm trying to replace all occurences of a multiline string in another string. Assuming that input contains the input text, output contains the resulting text, searchText contains the multiline string to be found and replaceText contains the replaced multiline string, I used this code:
output = input.Replace(searchText, replaceText);
The problem is that it works only with single line strings (that don't include newlines). How could I make it work for strings that contain newlines?
e.g.
searchText = "ABC\nDEF";
replaceText = "text";
input:
ABC
DEF
KLF
Z
output:
text
KLF
Z
You need to know what the new line is in the input. It could be LF only, but it could be CR+LF.
I am a little bit lazy to explain, so please read this Wikipedia about new line: https://en.wikipedia.org/wiki/Newline
So your problem might be because CR is also there, which makes that the string to search does not match at all. One solution is to set your search text as:
searchtext = "ABC" + System.Environment.NewLine + "DEF";
System.Environment.NewLine deals with the new line for you better. See the reference in msdn: https://msdn.microsoft.com/en-us/library/system.environment.newline(v=vs.110).aspx
I have a text variable which can contain many ((VAR=var_name)).
I want to replace ((VAR=var_name)) by the var_name only in src= (so, no in href= for instance.
sText = "some text href=\"sometext((VAR=XXXX))sometext((VAR=YYYYY))\"
src=\"sometext((VAR=XXXX))sometext((VAR=YYYYY)) \" some text some text
href=\"sometext((VAR=XXXX))sometext((VAR=YYYYY))\"
src=\"sometext((VAR=XXXX))sometext((VAR=YYYYY))\" some text";
I want to get after replacing :
sText = "some text href=\"sometext((VAR=XXXX))sometext((VAR=YYYYY))\"
src=\"sometext**XXXX**sometext**YYYYY**\" some text some text
href=\"sometext((VAR=XXXX))sometext((VAR=YYYYY))\"
src=\"sometext**XXXX**sometext**YYYYY**\" some text";
Have you got an idea ?
JC
string srcPattern = #"src="".+?""";
string varPattern = #"\(\(VAR=(.+?)\)\)";
string result = Regex.Replace(sText, srcPattern,
m => Regex.Replace(m.Value, varPattern, n => "**" + n.Groups[1].Value + "**"));
The first regular expression match the string src = "...". While the second regular expression replaces every statement in brackets on its value.
Doing this
String t = "asd\nasd";
TextBox objTxt = (TextBox)messageBox;
bjTxt.Text = t;
doesnt show
asd
asd
as expected, it shows
asdasd
why, its driving me crazy. TextBox is set to multiline and I can write lines in it manually. Thanks!
TextBox unlike Label and MessageBox ignores "\n" so if you want to get to the newline you will need to use "\r\n" combination. Nevertheless there is a better solution, just use Environment.NewLine and you won't need to think about \r\n combination for a newline.
Either:
String t = "asd\r\nasd";
Or:
String t = "asd" + Environment.NewLine + "asd";
The beautiful thing about Environment.NewLine is that there is no need to worry about the newline in any environment for which you are developing (or at least it should be that way).
EDIT:
I saw your comment, so I'll add few words. You could still use ReadToEnd() and if the text contains only "\n" for newline, you could do the following:
t = t.Replace("\n", "\r\n");
Or:
t = t.Replace("\n", Environment.NewLine);
since Environment.NewLine is essentially a string
Try to use Lines property of TextBox to assign all lines from file:
textBox.Lines = File.ReadAllLines(fileName);
And, as I stated in comment above, for your sample you should use Environment.NewLine for new line to appear in TextBox:
textBox.Text = "asd" + Environment.NewLine + "asd";
I want to convert more that spaces in a string to through c#?
Like if string is
My name is this.
then output should be
My name is this.
Replace the "regular" space with the "non-breaking space" Unicode character:
string outputString = "Input text".Replace(" ", "\u00A0");
Try with RegEx if you need to convert multiple spaces to a single non-breaking-space:
string convertedText =
new Regex("[ ]{2,}").Replace(textToConvert, " ");
Example:
My Name is this
^ ^^^ ^
It'll be changed to:
My Name is this
UPDATE
If you need to preserve extra spaces (and to replace with nbsp only multiple spaces) you may use this regex:
string convertedText =
new Regex(" (?= )|(?<= ) ").Replace(textToConvert, " ");
Example:
My Name is this
^ ^^^ ^
It'll be changed to:
My Name is this
For the second case, as alternative, you may even do not use regex at all (just loop) but they should be faster if you have to do it often with the same regex.
Correction the line below will not work
Please use Server.HtmlEncode for it
You will have to do it by code
string s = " ";
if(s == " ")
{
s = " "
}
Or use "My name is this".Replace(" ", " ");
Try this
string myString = "My name is this".Replace(" ", " ");
buildLetter.Append("</head>").AppendLine();
buildLetter.Append("").AppendLine();
buildLetter.Append("<style type="text/css">").AppendLine();
Assume the above contents resides in a file. I want to write a snippet that
removes any line which has empty string "" and put escape character before
the middle quotations. The final output would be:
buildLetter.Append("</head>").AppendLine();
buildLetter.Append("<style type=\"text/css\">").AppendLine();
The outer " .... " is not considered special chars. The special chars may be single
quotation or double quotation.
I could run it via find and replace feature of Visual Studio. However, in my case i
want it to be written in c# or VB.NET
Any help will be appreciated.
Perhaps this does what you want:
string s = File.ReadAllText("input.txt");
string empty = "buildLetter.Append(\"\").AppendLine();" + Environment.NewLine;
s = s.Replace(empty, "");
s = Regex.Replace(s, #"(?<="").*(?="")",
match => { return match.Value.Replace("\"", "\\\""); }
);
Result:
buildLetter.Append("</head>").AppendLine();
buildLetter.Append("<style type=\"text/css\">").AppendLine();