How can I output all the string arrays in a split string? - c#

I've written this code that splits "#" and ":"
var Split = Texts.Split(new char[] { '#' });
var Split1 = Texts.Split(new char[] { ':' });
I want to output all the string arrays to a file. I've tried and I get only one string, not all.
formatted = Split[0] + ":" + Split1[1];
File.WriteAllText(outputfile, formatted);
Here is my code:
public void CreateUsernameList(string targetfile,string outputfile)
{
string[] texts = File.ReadAllLines(targetfile);
string formatted = null;
foreach(string Texts in texts)
{
var Split = Texts.Split(new char[] { '#' });
var Split1 = Texts.Split(new char[] { ':' });
formatted = Split[0] + ":" + Split1[1];
File.WriteAllText(outputfile, formatted);
}
}

You are continuously overwriting the file in that loop. Instead collect the results in a List<string> and then write that to the file.
public void CreateUsernameList(string targetfile,string outputfile)
{
string[] texts = File.ReadAllLines(targetfile);
string formatted = null;
List<string> output = new List<string>();
foreach(string Texts in texts)
{
var Split = Texts.Split(new char[] { '#' });
var Split1 = Texts.Split(new char[] { ':' });
formatted = Split[0] + ":" + Split1[1];
output.Add(formatted);
}
File.WriteAllLines(outputfile, output)
}
An alternative that will not use as much memory would be
public void CreateUsernameList(string targetfile,string outputfile)
{
File.WriteAllLines(
outputfile,
File.ReadLines(targetfile)
.Select(line =>
{
var Split = line.Split(new char[] { '#' });
var Split1 = line.Split(new char[] { ':' });
return Split[0] + ":" + Split1[1];
}
)
);
}

As I dont see any format of data I can only guess that the error might be here:
formatted = Split[0] + ":" + Split1[1];
you are taking only single elements from each array of strings. try looping through all the elements in arrays Split and Split1 to print their values

It's better to read file in lazy manner:
File.ReadLines(targetfile).ForEach(line =>
{
File.AppendAllText("path", string.Join(":", Regex.Split(line, "#|:")
.Cast<Match>().Select(m => m.Value)));
});
static class ExtensionMethods
{
internal static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
{
if (enumerable == null) throw new NullReferenceException($"'{nameof(enumerable)}' argument is null");
using var enumerator = enumerable.GetEnumerator();
while (enumerator.MoveNext()) action(enumerator.Current);
}
}

Related

Deleting all empty elements from the end of a string array [duplicate]

I want to remove empty and null string in the split operation:
string number = "9811456789, ";
List<string> mobileNos = number.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(mobile => mobile.Trim()).ToList();
I tried this but this is not removing the empty space entry
var mobileNos = number.Replace(" ", "")
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
As I understand it can help to you;
string number = "9811456789, ";
List<string> mobileNos = number.Split(',').Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
the result only one element in list as [0] = "9811456789".
Hope it helps to you.
a string extension can do this in neat way as below
the extension :
public static IEnumerable<string> SplitAndTrim(this string value, params char[] separators)
{
Ensure.Argument.NotNull(value, "source");
return value.Trim().Split(separators, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim());
}
then you can use it with any string as below
char[] separator = { ' ', '-' };
var mobileNos = number.SplitAndTrim(separator);
I know it's an old question, but the following works just fine:
string number = "9811456789, ";
List<string> mobileNos = number.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
No need for extension methods or whatsoever.
"string,,,,string2".Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
return ["string"],["string2"]
The easiest and best solution is to use both StringSplitOptions.TrimEntries to trim the results and StringSplitOptions.RemoveEmptyEntries to remove empty entries, fed in through the pipe operator (|).
string number = "9811456789, ";
List<string> mobileNos = number
.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.ToList();
Checkout the below test results to compare how each option works,

Extract contents between specific tags in a textfile - C#

I have a text file with the following information:
ALLOC
apple1
orange1
banana1
ALLOC
apple2
orange2
banana2
ALLOC
apple3
orange3
banana3
Based on the help from stackflow community, I am now able to read the whole file.I also found out that to extract contents between a tag, for ex, ALLOC, I could write:
var filelocation = #"c:\Fruits.txt";
var sectionLines = File.ReadAllLines(filelocation).TakeWhile(l => !l.StartsWith("ALLOC"));
But this will give me IEnumerable<string>:
apple1
orange1
banana1
apple2
orange2
banana2
apple3
orange3
How do I create 3 separate strings as
string1 = apple1 orange1 banana1
string2 = apple2 ornage2 banana2
string3 = apple3 orange3
In short, need to extract contents between tags.
Here is some approach how you can return result which you want:
string[] words = { "ALLOC", "apple1", "orange1", "banana1", "ALLOC", "apple2", "orange2", "banana2", "ALLOC" };
var result = string.Join(" ", words)
.Split(new string[] { "ALLOC" }, StringSplitOptions.RemoveEmptyEntries)
.Select(p => p.Trim(' '));
First I am making single string of all words. Than I am splitting by "ALLOC", and selecting trimmed strings.
Result is:
string[] result = { "apple1 orange1 banana1", "apple2 orange2 banana2" };
For your case,
var filelocation = #"c:\Fruits.txt";
var allLines = File.ReadAllLines(filelocation);
var sectionLines = string.Join(" ", allLines)
.Split(new string[] { "ALLOC" }, StringSplitOptions.RemoveEmptyEntries)
.Select(p => p.Trim(' '));
This might do the trick for you
string fullstr = File.ReadAllText("c:\\Fruits.txt");
string[] parts = fullstr.Split(new string[] { "ALLOC" }, StringSplitOptions.RemoveEmptyEntries);
List<string> outputstr = new List<string>();
foreach(string p in parts)
{
outputstr.Add(p.Replace("\r\n", " ").Trim(' '));
}
Here we read all text at once using File.ReadAllText and then splitted it with ALLOC and then in the outputstr just added the splitted string by replacing \r\n that is new line with a space and trimmed the result.

Checking for and removing any characters in a string

I am wondering what would be the best way to specify an array of characters like,
{
}
[
]
and then check a string for these and if they are there, to completely remove them.
if (compiler.Parser.GetErrors().Count == 0)
{
AstNode root = compiler.Parse(phrase.ToLower());
if (compiler.Parser.GetErrors().Count == 0)
{
try
{
fTextSearch = SearchGrammar.ConvertQuery(root, SearchGrammar.TermType.Inflectional);
}
catch
{
fTextSearch = phrase;
}
}
else
{
fTextSearch = phrase;
}
}
else
{
fTextSearch = phrase;
}
string[] brackets = brackets = new string[]
{
"{",
"}",
"[",
"]"
};
string[] errorChars = errorChars = new string[]
{
"'",
"&"
};
StringBuilder sb = new StringBuilder();
string[] splitString = fTextSearch.Split(errorChars, StringSplitOptions.None);
int numNewCharactersAdded = 0;
foreach (string itm in splitString)
{
sb.Append(itm); //append string
if (fTextSearch.Length > (sb.Length - numNewCharactersAdded))
{
sb.Append(fTextSearch[sb.Length - numNewCharactersAdded]); //append splitting character
sb.Append(fTextSearch[sb.Length - numNewCharactersAdded - 1]); //append it again
numNewCharactersAdded++;
}
}
string newString = sb.ToString();
A regular expression can do this far more easily:
var result = Regex.Replace(input, #"[[\]()]", "");
Using a character set ([...]) to match anyone of the characters in it and replace with nothing. Regex.Replace will replace all matches.
Another concise way is using Enumerable.Except to get the set difference of the Chars(assuming brackets are chars):
String newString = new String(oldString.Except(brackets).ToArray());
string str = "faslkjnro(fjrmn){ferqwe}{{";
char[] separators = new []{'[', ']','{','}' };
var sb = new StringBuilder();
foreach (var c in str)
{
if (!separators.Contains(c))
{
sb.Append(c);
}
}
return sb.ToString();
How about this:
string myString = "a12{drr[ferr]vgb}rtg";
myString = myString.Replace("[", "").Replace("{", "").Replace("]", "").Replace("}", "");
You end up with:
a12drrferrvgbrtg
I don't know if I understand your problem, but you can solve your problem with this:
string toRemove = "{}[]";
string result = your_string_to_be_searched;
foreach(char c in toRemove)
result = result.Replace(c.ToString(), "");
or with an extension method
static class Extensions
{
public static string RemoveAll(this string src, string chars)
{
foreach(char c in chars)
src= src.Replace(c.ToString(), "");
return src;
}
}
With this you can use string result = your_string_to_be_searched.RemoveAll("{}[]");
string charsToRemove = #"[]{}";
string pattern = string.Format("[{0}]", Regex.Escape(charsToRemove));
var result = Regex.Replace(input, pattern, "");
The primary advantage of this over some of the other similar answers is that you aren't bothered with determining which characters need to be escaped in RegEx; you can let the library take care of that for you.
You can do this in a pretty compact fashion like this:
string s = "ab{c[d]}";
char[] ca = new char[] {'{', '}', '[', ']'};
Array.ForEach(ca, e => s = s.Replace(e.ToString(), ""));
Or this:
StringBuilder s = new StringBuilder("ab{c[d]}");
char[] ca = new char[] {'{', '}', '[', ']'};
Array.ForEach(ca, e => s.Replace(e.ToString(), ""));
Taken from this answer: https://stackoverflow.com/a/12800424/1498669
Just use .Split() with the char[] of your desired removeables and recapture it with .Join() or .Concat()
char[] delChars = "[]{}<>()".ToCharArray();
string input = "some (crazy) string with brac[et]s in{si}de";
string output = string.Join(string.Empty, input.Split(delChars));
//or
string output = string.Concat(input.Split(delChars));
References:
https://learn.microsoft.com/en-us/dotnet/csharp/how-to/parse-strings-using-split
https://learn.microsoft.com/en-us/dotnet/csharp/how-to/concatenate-multiple-strings#code-try-4

String Cleaning in C#

I am trying to write a function that as input takes a string containing words and removes all single character words and returns the new string without the removed characters
E.g.:
string news = FunctionName("This is a test");
//'news' here should be "This is test".
Can you please help?
Obligatory LINQ one-liner:
string.Join(" ", "This is a test".Split(' ').Where(x => x.Length != 1).ToArray())
Or as a nicer extension method:
void Main()
{
var output = "This is a test".WithoutSingleCharacterWords();
}
public static class StringExtensions
{
public static string WithoutSingleCharacterWords(this string input)
{
var longerWords = input.Split(' ').Where(x => x.Length != 1).ToArray();
return string.Join(" ", longerWords);
}
}
I'm sure there's a nicer answer using regex, but you could do the following:
string[] words = news.Split(' ');
StringBuilder builder = new StringBuilder();
foreach (string word in words)
{
if (word.Length > 1)
{
if (builder.ToString().Length ==0)
{
builder.Append(word);
}
else
{
builder.Append(" " + word);
}
}
}
string result = builder.ToString();
The interesting thing about this question is that presumably you also want to remove one of the spaces surrounding the single-letter word.
string[] oldText = {"This is a test", "a test", "test a"};
foreach (string s in oldText) {
string newText = Regex.Replace(s, #"\s\w\b|\b\w\s", string.Empty);
WL("'" + s + "' --> '" + newText + "'");
}
Output...
'This is a test' --> 'This is test'
'a test' --> 'test'
'test a' --> 'test'
With Linq syntax, you could do something like
return string.Join(' ', from string word in input.Split(' ') where word.Length > 1))
string str = "This is a test.";
var result = str.Split(' ').Where(s => s.Length > 1).Aggregate((s, next) => s + " " + next);
UPD
Using the extension method:
public static string RemoveSingleChars(this string str)
{
return str.Split(' ').Where(s => s.Length > 1).Aggregate((s, next) => s + " " + next);
}
//----------Usage----------//
var str = "This is a test.";
var result = str.RemoveSingleChars();

What is the best way to convert a string separated by return chars into a List<string>?

I need to often convert a "string block" (a string containing return characters, e.g. from a file or a TextBox) into List<string>.
What is a more elegant way of doing it than the ConvertBlockToLines method below?
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestConvert9922
{
class Program
{
static void Main(string[] args)
{
string testBlock = "line one" + Environment.NewLine +
"line two" + Environment.NewLine +
"line three" + Environment.NewLine +
"line four" + Environment.NewLine +
"line five";
List<string> lines = StringHelpers.ConvertBlockToLines(testBlock);
lines.ForEach(l => Console.WriteLine(l));
Console.ReadLine();
}
}
public static class StringHelpers
{
public static List<string> ConvertBlockToLines(this string block)
{
string fixedBlock = block.Replace(Environment.NewLine, "§");
List<string> lines = fixedBlock.Split('§').ToList<string>();
lines.ForEach(s => s = s.Trim());
return lines;
}
}
}
List<string> newStr = str.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList();
This will keep consecutive newlines as empty strings (see StringSplitOptions)
No need to convert to your special sign:
List<string> strings = str.Split(new string[] {Environment.NewLine}, StringSplitOptions.None).ToList();
strings.ForEach(s => s = s.Trim());
Have you tried splitting on newline/carriage return and using the IEnumerable ToList extension?
testBlock.Split( new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries )
.ToList()
If you want to keep empty lines but may have both linefeed and carriage return.
textBlock.Replace( "\r\n", "\n" ).Replace( "\r", "\n" ).Split( '\n' ).ToList();
Hmm. You know that string now has a .Split() that takes a string[] array, right?
So ...
string[] lines = data.Split(
new string[1]{ Environment.NewLine },
StringSplitOptions.None
);
ou can use RegEx.Split to split directly using the Enviroment.NewLine.
public static List<string> ConvertBlockToLines(this string block)
{
return Regex.Split(block, Environment.NewLine).ToList();
}
LINQ!
var linesEnum = testBlock.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable();
List<string> lines = linesEnum.ToList();

Categories

Resources