Regex for only some chars - c#

I have this text:
33113 1;3;\"windlight \"\"Feeling\"\"\r\nmetal, handmade\r\ninside: gold
metallic\r\noutisde: anthracite brushed\r\nH. 14 cm - B. 11,5
cm\";7,95;4001250331131;218,625;262,35;579;21;0004;0001;KUS\r\n
And this regex:
;\\"[^;]*\\";
Which gives the result:
;\"windlight \"\"Feeling\"\"\r\nmetal, handmade\r\ninside: gold metallic\r\noutisde: anthracite brushed\r\nH. 14 cm - B. 11,5 cm\";
Where I like to remove the new-line characters \r\n.Have you any ideas please?
I need something like this:
var replaced = Regex.Replace(
"a regex pattern which removes \r\n from the selected text", " ");
This is what I want to remove from text:
http://postimg.org/image/5hbtd1czx/

So for example,
String str = "Text contains \r \n string here";
str = str.Replace("(\r\n", " "); // with blank space

You can try to use replace:
myString = text.Replace("\r\n", "")
EDIT:
myString = text.Replace(System.Environment.NewLine, " ")
or
myString = text.Replace("\r\n", " ").Replace("\r", " ").Replace("\n", " ");
Also note that
string[] myString = File.ReadAllLines(yourTextFile);
This will automatically remove all the \n and \r from your text.

Related

How to Fix Malformed Whitespace Separator using C#

I have the following string:
string testString = ",,,The,,,boy,,,kicked,,,the,,ball";
I want to remove the unwanted commas and have the sentence as this (simply printed to the console):
The boy kicked the ball
I tried the below code:
string testString = ",,,The,,,boy,,,kicked,,,the,,ball";
string manipulatedString = testString.Replace(",", " "); //line 2
Console.WriteLine(manipulatedString.Trim() + "\n");
string result = Regex.Replace(manipulatedString, " ", " ");
Console.WriteLine(result.TrimStart());
However, I end up with a result with double whitespaces as so:
The boy kicked the ball
It kind of makes sense why I am getting such an anomalous output because in line 2 I am saying that for every comma (,) character, replace that with whitespace and it will do that for every occurrence.
What's the best way to solve this?
This is a simple solution using Split and Join
string testString = ",,,The,,,boy,,,kicked,,,the,,ball";
var splitted = testString.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
string result = string.Join(" ", splitted);
Console.WriteLine(result);
You could use regex to replace the pattern ,+ (one or more occurrences of a comma) by a whitespace.
var replacedString = Regex.Replace(testString, ",+", " ").Trim();
Added Trim to remove white spaces at beginning/end as I assume you want to remove them.

How to prevent adding suffix in blank lines of large string? C#

I want to add prefix to each line of large string excluding blank lines. Below code adds prefix to blank lines as well. I have searched many websites but I am unable to find proper solution to do that. here is my code:
string txt_input="abc \n \n efg \n \n \n hij";
string source = string.Join(Environment.NewLine, txt_input);
string result = string.Join(Environment.NewLine, source
.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)
.Select(line => txt_prefix + line));
Your question is a bit fuzzy so I'll work on the assumption that your input looks like the txt_input you provided:
string txt_input="abc \n \n efg \n \n \n hij";
and you want an output of the sort:
var ouput = "myPrefix_abc \n \n myPrefix_efg \n \n \n myPrefix_hij";"
Something like this should work:
string prefix = "myPrefix_";
string txt_input="abc \n \n efg \n \n \n hij";
var split = txt_input.Split(new[] {"\n"}, StringSplitOptions.None);
var prefixedList = new List<string>();
foreach(var line in split){
if(string.IsNullOrWhiteSpace(line)) {
prefixedList.Add(line);
continue;
}
prefixedList.Add(prefix + line);
}
var output = string.Join("\n", prefixedList.ToArray());
Console.WriteLine(output);
If you actually want to see the \n chars replace the second last line with
var output = string.Join("\\n", prefixedList.ToArray());
(note the second '\' before the '\n')

Replace after a string - Before a character

I have a string like :
string str = "First Option: This is a text. This is second text.";
I can replace This is a text. with :
str = str.Replace("This is a text.", "New text");
But my constant word is First Option: and This is a text is not constant so how can replace the text after First Option: until occurring . (it means before This is second text.).
In this example the expecting result is :
First Option: New text. This is second text.
One option is to use Regex.Replace instead:
str = Regex.Replace(str, #"(?<=First Option:)[^.]*", "New text");
(?<=First Option:)[^.]* matches a sequence of zero or more characters other than dot '.', preceded by First Option: via a positive look-behind.
Not the shortest but if you want to avoid regular expressions:
string replacement = "New Text";
string s = "First Option: This is a text.This is second text.";
string[] parts = s.Split('.');
parts[0] = "First Option: " + replacement;
s = string.Join(".", parts);
Look up .IndexOf() and Substring(...). That will give you what you need:
const string findText = "First Option: ";
var replaceText = "New Text.";
var str = "First Option: This is a text. This is second text.".Replace(findText, "");
var newStr = findText + str.Replace(str.Substring(0, str.IndexOf(".") + 1), replaceText);
Console.WriteLine(newStr);

How to remove white spaces from sentence?

I want to remove all white spaces from string variable which contains a sentence.
Here is my code:
string s = "This text contains white spaces";
string ns = s.Trim();
Variable "sn" should look like "Thistextcontainswhitespaces", but it doesn't(method s.Trim() isn't working). What am I missing or doing wrong?
The method Trim usually just removes whitespace from the begin and end of a string.
string s = " String surrounded with whitespace ";
string ns = s.Trim();
Will create this string: "String surrounded with whitespace"
To remove all spaces from a string use the Replace method:
string s = "This text contains white spaces";
string ns = s.Replace(" ", "");
This will create this string: "Thistextcontainswhitespaces"
Try this.
s= s.Replace(" ", String.Empty);
Or using Regex
s= Regex.Replace(s, #"\s+", String.Empty);

How to convert a space into non breaking space entity in c#

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

Categories

Resources