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')
Related
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.
I need to remove everything in a string before the first occurrence of a space.
Every string starts with a number and followed by a space
Replace the number and the space, thus leaving the rest of the string in tact
For Example:
22 The cats of India
4 Royal Highness
562 Eating Potatoes
42 Biscuits in the 2nd fridge
2564 Niagara Falls at 2 PM
I just need:
The cats of India
Royal Highness
Eating Potatoes
Biscuits in the 2nd fridge
Niagara Falls at 2 PM
Basically remove every number before the first space, including the first space.
I tried this:
foreach (string line in lines)
{
string newline = line.Trim().Remove(0, line.IndexOf(' ') + 1);
}
This works for numbers below 10. After it hits 2 digits, it doesn't work properly.
How should I change my code?
If you want to make sure you only match digits at the beginning of the string, you can use the following regex:
^\d+\p{Zs}
See demo
Declare it like:
public static readonly Regex rx = new Regex(#"^\d+\p{Zs}", RegexOptions.Compiled);
The ^\d+\p{Zs} regex means: one or more digits at the start of the string followed with 1 whitespace.
And then use it like
string newline = rx.Replace(line, string.Empty);
EDIT: To make sure the line has no leading whitespace, we can add .Trim() to strip it like:
Regex rx = new Regex(#"^\d+\p{Zs}", RegexOptions.Compiled);
string newline = rx.Replace(line.Trim(), string.Empty);
I know you already found a resolution to your issue. But I am going to explain why your code didn't work in the first place.
Your data has extra spaces which is why you are trimming it: line.Trim(). But the real problem lies in the the following statement:
string newline = line.Trim().Remove(0, line.IndexOf(' ') + 1);
You are making the assumption about the order of the operation and the fact that string data type is not immutable. When the operation of Trim() function is complete it returns a whole new string which is used in the Remove() operation. But the IndexOf() function is done on the original line of data.
So the correct line of code would be the following:
foreach (string line in lines)
{
// trim the line first
var temp = line.Trim();
// now perform all operation on the new temporary string
string newline = temp.Remove(0, temp.IndexOf(' ') + 1);
// debugging purpose
Console.WriteLine(newline);
}
Another solution:
var lines = new string[]
{
"22 The cats of India",
"4 Royal Highness",
"562 Eating Potatoes",
"42 Biscuits in the 2nd fridge",
"2564 Niagara Falls at 2 PM"
};
foreach (var line in lines)
{
var newLine = string.Join(" ", line.Split(' ').Skip(1));
}
Use a regex like so:
string newline = Regex.Replace(line, #"^\s*\d+\s*", "");
This will remove numbers only, not other text before the first space.
This is what you are looking for
foreach (string line in lines)
{
string newline = line.Replace(line.Split(new Char[]{' '})[0] + ' ',string.Empty);
}
UPDATE
string search=line.Split(new Char[]{' '})[0];
int pos=line.indexOf(search);
string newline = line.Substring(0, pos) + string.Empty + line.Substring(pos + search.Length);
FULL CODE
using System;
public class Program
{
public static void Main()
{
var lines = new string[]
{
"22 The cats of India",
"4 Royal Highness",
"562 Eating Potatoes",
"42 Biscuits in the 2nd fridge",
"2 Niagara Falls at 2 PM"
};
foreach(string line in lines){
string search=line.Split(new Char[]{' '})[0];
int pos=line.IndexOf(search);
string newline = line.Substring(0, pos) + string.Empty + line.Substring(pos + search.Length);
Console.WriteLine(newline);
}
}
}
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.
I have a string:
string data = "SEQUENCE $FIRST$ THEN $SECOND$ AND FINALLY \\$12345";
I want to split it up using Regex using the "$" character. However, I want to use \ as escape character.
string[] sComponents = Regex.Split(data, "(\\$)", RegexOptions.ExplicitCapture);
By running the code above I would get:
sComponents[0] = "SEQUENCE "
sComponents[1] = "FIRST"
sComponents[2] = " THEN "
sComponents[3] = "SECOND"
sComponents[4] = " AND FINALLY "
sComponents[5] = "12345"
But I want sComponents[4] to contain the $ such as " AND FINALLY $12345"
What is the best way to achieve this, does Regex has some type of escape character when splitting? or I have to manually handle this before I call Regex Split with my own logic?
Basically it comes down to, if Regex sees "$" then split but if it sees "\\$" then ignore it don't split at this very position.
Just split the input string according to the below regex which uses negative lookahead.
\$(?!\d)
Code:
string value = "SEQUENCE $FIRST$ THEN $SECOND$ AND FINALLY $12345";
string[] lines = Regex.Split(value, #"\$(?!\d)");
foreach (string line in lines) {
Console.WriteLine(line);
IDEONE
Update:
Use the below regex to split the input according to the $ symbol which is not preceeded by two backslashes.
(?<!\\\\)\$
Code:
string value = "SEQUENCE $FIRST$ THEN $SECOND$ AND FINALLY \\\\$12345";
string[] lines = Regex.Split(value, #"(?<!\\\\)\$");
foreach (string line in lines) {
Console.WriteLine(line);
IDEONE
How do i search a string for a newline character? Both of the below seem to be returning -1....!
theJ = line.IndexOf(Environment.NewLine);
OR
theJ = line.IndexOf('\n');
The string it's searching is "yo\n"
the string i'm parsing contains this "printf("yo\n");"
the string i see contained during the comparison is this: "\tprintf(\"yo\n\");"
"yo\n" // output as "yo" + newline
"yo\n".IndexOf('\n') // returns 2
"yo\\n" // output as "yo\n"
"yo\\n".IndexOf('\n') // returns -1
Are you sure you're searching yo\n and not yo\\n?
Edit
Based on your update, I can see that I guessed correctly. If your string says:
printf("yo\n");
... then this does not contain a newline character. If it did, it would look like this:
printf("yo
");
What it actually has is an escaped newline character, or in other words, a backslash character followed by an 'n'. That's why the string you're seeing when you debug is "\tprintf(\"yo\\n\");". If you want to find this character combination, you can use:
line.IndexOf("\\n")
For example:
"\tprintf(\"yo\\n\");" // output as " printf("yo\n");"
"\tprintf(\"yo\\n\");".IndexOf("\\n") // returns 11
Looks like your line does not contain a newline.
If you are using File.ReadAllLines or string.Split on newline, then each line in the returned array will not contain the newline. If you are using StreamReader or one of the classes inheriting from it, the ReadLine method will return the string without the newline.
string lotsOfLines = #"one
two
three";
string[] lines = lotsOfLines.Split('\n');
foreach(string line in lines)
{
Console.WriteLine(line.IndexOf('\n'); // prints -1 three times
}
That should work although in Windows you'll have to search for '\r\n'.
-1 simply means that no enter was found.
It depends what you are trying to do. Both may no be identical on some platforms.
Environment.NewLine returns:
A string containing "\r\n" for non-Unix platforms, or a string
containing "\n" for Unix platforms.
Also:
If you want to search for the \n char (new line on Unix), use \n
If you want to search for the \r\n chars (new line on Windows), use \r\n
If your search depend on the current platform, use Environment.NewLine
If it returns -1 in both cases you mentioned, then you don't have a new line in your string.
When I was in college and I did a WebForms aplication to order referencies.
And the line break/carriage return it was what I used to break a referense.
//Text from TextBox
var text = textBox1.Text;
//Create an array with the text between the carriage returns
var references = text.Split(new string[] { "\r\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
//Simple OrderBy(Alphabetical)
var ordered = references.ToList<string>().OrderBy(ff => ff);
//Return the entry text ordered alphabetical em with a carriage return between every result
var valueToReturn = String.Join(Environment.NewLine, ordered);
textBox1.Text = valueToReturn;
The Environment.NewLine is not the same as \n. It is a CRLF (\r\n). However, I did try with the \n using IndexOf and my test did find the value. Are you sure what you're searching for is a \n rather than a \r? View your text in hexadecimal format and see what the hex value is.