So i have a txt file with names which i provided at the top of the code. The problem is i want to split those lines into seperate lines if they contain a ' ' (space)
John
James Peter
Mary Bob
Thomas
Michael
i want it to be like this:
John
James
Peter
Mary
Bob
Thomas
Michael
.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
#region test2
StreamReader Fixed = new StreamReader("SSfile.txt");
List<string> FixedList = new List<string>();
List<string> FixedList2 = new List<string>();
FixedList2.ToArray();
string ReadFixed = Fixed.ReadLine();
while (ReadFixed != null)
{
FixedList.Add(ReadFixed);
ReadFixed = Fixed.ReadLine();
}
Fixed.Close();
for (int i = 0; i < FixedList.Count(); i++)
{
FixedList[i].Split(' ');
}
Console.WriteLine(FixedList.ToString());
Console.ReadKey();
#endregion
Console.ReadKey();
}
}
}
So what should i do??
Use the power of LINQ:
String.Split will split a string into an array of substrings based on the given delimiter.
SelectMany will "flatten" a collection. That means you give a method that transforms one item into many items (for example one string into an array of string) and SelectMany will form a single enumerable from it.
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
var names = File.ReadLines("SSfile.txt")
.SelectMany(line => line.Split(' '));
foreach(var name in names)
{
Console.WriteLine(name);
}
}
}
}
Split each line, example:
string.Join("\n","James Peter".Split(' '));
Related
I have an array of strings, like in the bellow example:
string[] words = { "C#", "I like C#",
"My string is this",
"Just words", "Delegates and Linq"};
To count the words in each of the strings it's pretty straight forward, one can use words.Split(' ').Length, and a foreach creating an array containing the number of words in each string, or place the individual counts directly into an array, let's call it an array of counts, with query syntax:
var countWordsArray = from s in words select s.TrimEnd(' ').Split(' ').Length;
What I would like to do instead is to use extension methods, something like:
var CountWordsArray = words.Select(s => s...);
The hours are long but the day is short, so I'd realy appreciate the help. I'm sure I'm missing something basic but I can't quite put my finger on it.
Extension method translation:
var listaUmCountChars = words.Select(s => s.Split(' ').Length);
int wordCount =
words.Sum((w) => w.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Length);
You could create an extension method to do the word count too.
using System.Collections.Generic;
using System.Linq;
public static class StringExtensions
{
public static int WordCount(this string me)
{
return me.Split(' ').Length;
}
}
class Program
{
static void Main(string[] args)
{
string[] words = { "C#", "I like C#",
"My string is this",
"Just words", "Delegates and Linq"};
List<int> listaUmCountChars = words.Select(s => s.WordCount()).ToList(); // 1 3 4 2 3
int totalWordCount = words.Sum(s => s.WordCount()); // 13
}
}
I have many filenames such as:
libgcc1-5.2.0-r0.70413e92.rbt.xar
python3-sqlite3-3.4.3-r1.0.f25d9e76.rbt.xar
u-boot-signed-pad.bin-v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57.rbt.xar
I need to reliably extract the name, version and "rbt" or "norbt" from this. What is the best way? I am trying regex, something like:
(?<fileName>.*?)-(?<version>.+).(rbt|norbt).xar
Issue is the file name and version both can have multiple semi colons. So I am not sure if there is an answer by I have two questions:
What is the best strategy to extract values such as these?
How would I be able to figure out which version is greater?
Expected output is:
libgcc1, 5.2.0-r0.70413e92, rbt
python3-sqlite3, 3.4.3-r1.0.f25d9e76, rbt
u-boot-signed-pad.bin, v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57, rbt
This will give you what you want without using Regex:
var fileNames = new List<string>(){
"libgcc1-5.2.0-r0.70413e92.rbt.xar",
"python3-sqlite3-3.4.3-r1.0.f25d9e76.rbt.xar",
"u-boot-signed-pad.bin-v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57.rbt.xar"
};
foreach(var file in fileNames){
var spl = file.Split('-');
string name = string.Join("-",spl.Take(spl.Length-2));
string versionRbt = string.Join("-",spl.Skip(spl.Length-2));
string rbtNorbt = versionRbt.IndexOf("norbt") > 0 ? "norbt" : "rbt";
string version = versionRbt.Replace($".{rbtNorbt}.xar","");
Console.WriteLine($"name={name};version={version};rbt={rbtNorbt}");
}
Output:
name=libgcc1;version=5.2.0-r0.70413e92;rbt=rbt
name=python3-sqlite3;version=3.4.3-r1.0.f25d9e76;rbt=rbt
name=u-boot-signed-pad.bin;version=v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57;rbt=rbt
Edit:
Or using Regex:
var m = Regex.Match(file,#"^(?<fileName>.*)-(?<version>.+-.+)\.(rbt|norbt)\.xar$");
string name = m.Groups["fileName"].Value;
string version = m.Groups["version"].Value;
string rbtNorbt = m.Groups[1].Value;
The output will be the same. Both approaches assum that "version" has one -.
Tested following code and work perfectly with Regex. I used option Right-To-Left
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication107
{
class Program
{
static void Main(string[] args)
{
string[] inputs = {
"libgcc1-5.2.0-r0.70413e92.rbt.xar",
"python3-sqlite3-3.4.3-r1.0.f25d9e76.rbt.xar",
"u-boot-signed-pad.bin-v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57.rbt.xar"
};
string pattern = #"(?'prefix'.+)-(?'middle'[^-][\w+\.]+-[\w+\.]+)\.(?'extension'[^\.]+).\.xar";
foreach (string input in inputs)
{
Match match = Regex.Match(input, pattern, RegexOptions.RightToLeft);
Console.WriteLine("prefix : '{0}', middle : '{1}', extension : '{2}'",
match.Groups["prefix"].Value,
match.Groups["middle"].Value,
match.Groups["extension"].Value
);
}
Console.ReadLine();
}
}
}
I have a input list that takes input in the above format and put them into a comma seperated string. I would like to get strings before and after colon(:).
I tried this regex pattern
string[] reg = Regex.Split(x, #"^(?:[\w ]\:\s[\w]+)+$");
but it doesnt seem to work. Please help.
Below is my code. This is a C# console application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
List<string> input = new List<string>();
Console.WriteLine("Please enter your input");
string readinput = Console.ReadLine();
input.Add(readinput);
while (readinput != "")
{
readinput = Console.ReadLine();
input.Add(readinput);
}
string x = string.Join(",", input.ToArray());
Console.WriteLine(x);
// using regex
string[] reg = Regex.Split(x, #"^(?:[\w ]\:\s[\w]+)+$");
Console.WriteLine(reg);
Console.ReadLine();
}
}
}
Sorry i was not very clear but the
input : Amby : Dexter,
Dexter : Karla,
Karla : Matt .....
Expected Output is Amby, Dexter, Karla, matt....
If I understood you correctly... User enters some strings, and then you join them with commas. After that you want to split that string by colons?
Why don't you use simpler solution like this:
string[] reg = x.Split(':').Select(s => s.Trim()).ToArray();
Maybe this will get you started:
new Regex(#"(([a-zA-Z])+(?:[\s\:\,]+))").Matches("...");
or this regex
"\b([a-zA-Z])+\b"
Iterate over the MatchCollection.
I am relatively new to C# programming and I apologize if this is a simple matter, but I need help with something.
I need a function which will 'extract' regular AND decimal numbers from a string and place them in an array. I'm familiar with
string[] extractData = Regex.Split(someInput, #"\D+")
but that only takes out integers. If I have a string "19 something 58" it will take 19 and 58 and store them into two different array fields. However if I had "19.58 something" it will again take them as two separate numbers, while I want to register it as one decimal number.
Is there a way to make it 'read' such numbers as one decimal number, using Regex or some other method?
Thanks in advance.
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication9
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string[] inputs = {
"9 something 58" ,
"19.58 something"
};
foreach (string input in inputs)
{
MatchCollection matches = Regex.Matches(input, #"(?'number'\d*\.\d*)|(?'number'\d+[^\.])");
foreach (Match match in matches)
{
Console.WriteLine("Number : {0}", match.Groups["number"].Value);
}
}
Console.ReadLine();
}
}
}
Try this
Regex.Replace(someInput, "[^-?\d+\.]", ""))
i am reading a book named "Visual C# 2012 Programming" and i came up with following code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch05StringManupulationEx
{
class Program
{
static void Main(string[] args)
{
string myString = "String with small s";
char[] myChar = myString.ToCharArray();
foreach (char whatever in myString)
{
Console.WriteLine("{0}", whatever);
}
Console.Write("\nyou have entered {0} characters in String ",myString.Length);
Console.ReadKey();
}
}
}
i don't know what the aurthor is doing on line :
char[] myChar = myString.ToCharArray();
because he is not using the variable named myChar anywhere in the code and even though i commented the line and compiled the program the output is same, can any one explain what is the purpose of this line in this code ?
Probably they forgot to remove that line or show what does it do, It's an array of character, A string is full of characters, each letter of a string is a character, you can access any of these arrays by using a zero based numbers, for example:
string a = "Hello";
// Prints e
Console.WriteLine(a[2]);
You can change this line to myChar to understand, It's same to an array of string, Which means a string is an array of chars, here's the example:
foreach (char whatever in myChar)
{
Console.WriteLine("{0}", whatever);
}