Counting frequency of characters in a line from a .txt file [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a C# task to do and I am stuck on part of the coding. I am using StreamReader to read a .txt file which contains group exam grade data (e.g. ADCBCBBBADEBCCBADBAACDCCBEDCBACCFEABBCBBBCCEAABCBB), I need the code to work out how many A's, B's etc there are inside each set of data, I thought about using some form of count++ code but each attempt just throws errors.
I want it to print onto console the number of A's in that line of the .txt file.
I hope that makes sense, I understand how to do the rest but I just needed a hand on this section.

Consider using System.Linq, eg...
string myString = "ADCBCBBBADEBCCBADBAACDCCBEDCBACCFEABBCBBBCCEAABCBB";
int countOfAs = myString.Count(x => x == 'A');
//Result: 9

Related

How to edit CSV data in C# like 1234567 to 123-4567? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Im trying to edit a postcode data which comes as for example 1234567 and i want to put a dash between 123 and 4567 (e.g 123-4567) is it possible to do this within C# without editing the csv file itself?
Im using a generic for loop to read through the csv file but i want to edit it like i asked above.
foreach(string line in csvData_){
string[] col = line.Split(',');
deliveryAddress.Text = col[0];
deliveryPostcode.Text = col[1];
deliveryPostcode contains 1234567 for e.g.
To solve your problem, you can use the method Insert() of the string object.
For example:
deliveryPostcode.Text = col[1].Length == 7 ? col[1].Insert(3, "-") : col[1];
You have to fit it for your needs, but this is the way I would use.
Here is the MSDN page of the String.Insert().

How to create .list file in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I know how to put string list to a .txt file for example:
//nw_TCList is a string list
string nwFinal = String.Join("\n", nw_TCList.ToArray());
System.IO.File.WriteAllText(nwPath, nwFinal);
So my question is how to put this string list file to a .list file?
-------------Question update
Thank you for all the reply, nwPath is my txt file address. (#"c:\abc\name.txt")
Problem solved! just change .txt to .list, LOL, i was thinking how to convert txt file to list file before, haha.
.list is just Extension so don't worry about that
there is direct method to write Array to file
System.IO.File.WriteAllLines(nwPath, nw_TCList);
And If you want to have .list Extension
give that in path param
E.g.
System.IO.File.WriteAllLines("D://Data.list", nw_TCList);
that did trick for me

Grabbing part of a String [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a string that I am grabbing that is input as:
05/22/2015
Eligibility
05/06/2015
Date of Death
I need 05/06/2015. The dates will change as the program runs through a database, and I am just a little unsure on how to always be grabbing the correct one.
So you need the second date only? Is it always going to be the 3rd line? If so you can do
var secondDate = myData.Split(new [] { Environment.NewLine }, StringSplitOptions.None)[2];
If the new line isn't for certain; /n vs /r/n, use:
var secondDate = myDate.Split(Environment.Newline.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[2];
If I understand you correctly, your string has 4 lines and you want the part of the string between linebreak 2 and 3.
Search for the positions of the second and third linebreak \n and use the substring derived of these positions.

C# - How to assign different lines of File.ReadAllText to individual variables? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I've got is a text file with some (hypothetical) students and their attendance.
Looks like this:
Stud1
LOOOALLOOAAL
Stud2
OOOOOOOAOOOO
Stud3
LLLLOOOOALLA
So what I want to do, is assign say Stud1 to show Stud1: LOOOALLOOAAL etc etc.
Any ideas?
actually your post is kind of "unclear" to readers but I did get your point regarding reading lines from text to variables
I suggest you use File.ReadAllLines, then format your text with each values separated per line
so it goes like this
Source.txt
LOOOALLOOAAL
OOOOOOOAOOOO
LLLLOOOOALLA
then your line of code will be
string[] students = File.ReadAllLines("Source.txt");
thus the values would be
students[0] = LOOOALLOOAAL
students[1] = OOOOOOOAOOOO
students[2] = LLLLOOOOALLA
the code File.ReadAllLines() gets every line of text from your source file until it reaches the endline and stores it into an array of string. if you needed your values for calculations, then you'll need to parse such values
I just showed you how to get values from text to variables. how you show them as output will be on you.

Find the number of characters that are present in every line into a List<string> [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to find the number of characters that are present in every line, within a List:
So, if for example, the string is
Ajdahnfj
Jnbafdbn
Jadnjadg
a,j,d,n are common for each line. Therefore the answer is 4.
No case sensitive is required
Regards
Here's a high level description of what I would do:
Make a collection of characters
initialize it to represent all of the characters in the first line
for each other line,
for each character in the collection, check if it's in that line. if it isn't then remove it from the list
return the resulting collection of characters

Categories

Resources