I have a text file that has a string like:
"•5•Enter Title Name•Enter Description Here•30/04/2015•UNFINISHED•"
I am reading this text file in, how can I write it so the items between the • are put in seperate variables or strings. I have a general idea, using foreach loops with an if statement that checks for the specific character. Can anybody help me please? (code so far is below)
MessageBox.Show("Opening saved file: TaskFile.txt");
string path1 = (#"TaskFile.txt");
string lineOfText = File.ReadAllText(path1);
System.Diagnostics.Debug.WriteLine(lineOfText);
foreach (var sItem in lineOfText)
{
if(sItem == '•')
{
System.Diagnostics.Debug.WriteLine("test");
}
}
You can use Split method and use it like this
List<string> list = sItem.Split('.').ToList();
I agree with Ehsan on the Split method, but I think it should be like this:
var arrayOfStrings = lineOfText.Split('•');
foreach (var str in arrayOfStrings)
{
//do something.
}
Related
I'd like to get all class name icon from fontawesome to list or array in c# from fontawesome.
I'd like to get all variants icon for example:
fas fa-abacus
far fa-abacus
fal fa-abacus
...
I tried to extract it from the css file, but it only gets the icon names themselves without prefixes.
var text = File.ReadAllText(#"fontawesome\all.css");
var allMatches = Regex.Matches(text, #"^\.fa\-(.*):", RegexOptions.Multiline);
Thank's for help.
Monika
public List<string> ListOfFontAwesomeIcons(string FilePath)
{
List<string> response = new List<string>();
List<string> fontAwesomePrefixes = new { "fas", "far" /*manually add all prefixes*/ }
var textLines = File.ReadAllLines($"{FilePath}");
foreach(string l in textLines)
{
foreach(string p in fontAwesomePrefixes)
{
if(l.Contains(p))
response.Add(l.Replace($"{p} "));
}
}
return response;
}
I ran this in C# 9, VS2019 just based off of feeding it a test string, and it worked for me. I double-checked for fa- in the result list to make sure I excluded all of the other results.
Also, I just converted to List of strings for display. You don't really need to do that, depending on what you're doing with your results.
EDIT: Code updated to reflect FA's .css file available on their website, not the format of the string I was using earlier.
Replace the path in the first line with whatever reflects your project. Note that there are faster methods than ReadAllText, but it should be fine for this.
string text = System.IO.File.ReadAllText("./wwwroot/css/all.css");
var allMatches = Regex.Matches(text, "(?<=[.]).*?(fa-)?.*?(?=[:])", RegexOptions.None);
List<string> allMatchesStrings = new();
foreach (var item in allMatches)
{
if (item.ToString().Contains("fa-"))
allMatchesStrings.Add(item.ToString());
}
foreach (var item in allMatchesStrings)
{
Console.WriteLine(item);
}
I have this code which is suppose to display all lines which contains a specifies string but instead of returning all lines with that string it only returns last line which contains the string. How can I make it to display all lines?
if(bookingType == "Express")
{
string stringToSearch = #"Express";
string[] lines = File.ReadAllLines(#"pathway");
foreach (string line in lines)
{
if (line.Contains(stringToSearch))
{
lstAvailableTrains.Items.Clear();
lstAvailableTrains.Items.Add(line);
}
}
}
You are clearing the items in each time.You need to move clear out of the loop
lstAvailableTrains.Items.Clear();
foreach (string line in lines)
{
if (line.Contains(stringToSearch))
{
lstAvailableTrains.Items.Add(line);
}
}
Use LINQ, and set the ListBox's ItemsSource property:
using System.Linq;
...
lstAvailableTrains.ItemsSource =
File.ReadAllLines("pathway").Where(line => line.Contains(stringToSearch));
I have a List<string> that get's populated with URLs. What I'd like to do is convert the contents of the List to hyperlinks that the user can click on. I've seen a bunch of examples of how to do this, but most of them were to insert in to an email, or switch the word here to a hyperlink. I just don't know what I'm looking at, so it's a little confusing. Here's what I have:
List<string> lstUrls = new List<string>();
//PROGRAM GETS URLS FROM ELEMENTS IN HERE....
foreach (string s in lstUrls)
{
s = ""; //THIS DOESN'T WORK...
}
I don't want to change the content of the string - just to be able to display as a hyperlink. For example, one string value will be https://www.tyco-fire.com/TD_TFP/TFP/TFP172_02_2014.pdf; and how Stack Overflow displays it as a link, that's what I would like to accomplish.
I know I'm obviously botching the syntax. Any help is appreciated.
You can´t change the content of a List<T> while iterating it using foreach. But you can using for:
for(int i = 0; i < lstUrls.Count; i++)
{
var s = lstUrls[i];
lstUrls[i] = "" + s + "";
}
A bit easier to read was this:
lstUrls[i] = String.Format("{0}", s);
You could use linq for it:
lstUrls = lstUrls.Select(s => $"").ToList();
Or rather displaying the url in it:
lstUrls = lstUrls.Select(s => $"{s}").ToList();
I'd like to be able to collect all the string values in a Winform ListBox. At the moment my code loops through the ListBox and gets the values, but it's appending all values together in one long string:
private string GetFormNumberValues()
{
string formNumbers = "";
foreach (string item in this.lbFormNumbers.Items)
{
formNumbers += item.ToString();
}
return formNumbers;
}
How can I collect each individual string value to use for later? Thanks.
You can have them in a List this way:
var list = listBox1.Items.Cast<object>().Select(x => x.ToString()).ToList();
Try something like this:
private string[] GetFormNumberValues()
{
List<string> strings = new List<string>();
foreach (string item in this.lbFormNumbers.Items)
{
strings.Add(item.ToString());
}
return strings.ToArray();
}
(Depending on your needs, you could simplify this by returning a List rather than an array...)
I am using this foreach loop to search for files in a directory and then read them.
foreach (string file in Directory.EnumerateFiles(location, "*.MAI"))
Inside this loop I want to search for the line in the file that contains the word "Sended". Is there a way to look for this word and then read that line?
Try it:
var location = #"<your location>";
foreach (string file in Directory.EnumerateFiles(location, "*.MAI"))
{
var findedLines = File.ReadAllLines(file)
.Where(l => l.Contains("Sended"));
}
If you work with big files, you should use ReadLines method, because when you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array.
Another example from msdn:
var files = from file in Directory.EnumerateFiles(location, "*.MAI")
from line in File.ReadLines(file)
where line.Contains("Sended")
select new
{
File = file,
Line = line
};
Full information, look here: https://msdn.microsoft.com/library/dd383503.aspx
If .MAI files are Textfiles try the following:
foreach (string file in Directory.EnumerateFiles(location, "*.MAI"))
{
foreach (string Line in File.ReadAllLines(file))
{
if (Line.Contains("Sended"))
{
//Do your stuff here
}
}
}