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 5 years ago.
Improve this question
The requirement is that a user can choose a file name format from predefined tags.
Example
InvoiceNo Date VendorName
if user chooses InvoiceNo-Date-Vendorname then the file name should be generated like: 001-20170512-ABCElectronics
if user chooses InvoiceNo-Date then the file name should be generated like: 001-20170512
if user chooses VendorName-InvoiceNo-Date then the file name should be generated like: ABCElectronics-001-20170512
format.Replace("InvoiceNo",generateNo());
Will .Replace first check if string exists and then executes the 2nd parameter? The 2nd parameter could be a long running method.
Should I first check if tag exists in the file format and then replace or just use .Replace method without checking?
Thanks
simple way:
input = input.Replace("InvoiceNo",generateInvo());
input = input.Replace("Date",generateDate());
input = input.Replace("Vendorname",generateVendor());
this will change the first occurrence of those strings for the code you desire.
You can also do this in one line like x.Replace(y0,y1).Replace(z0,z1); if you wish.
Related
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 last year.
Improve this question
I'm using csharp in console.net
I want to get a txt file from the user as input
But I do not want the text inside the txt file to be displayed to the user
Instead, I want the text to be hidden and the number of lines to be displayed instead
If I understood you fine, you want to count the number of lines in the txt file, right?
A basic approach could be:
string[] fileLines = System.IO.File.ReadAllLines("<path to the file>");
if (fileLines != null)
Console.WriteLine($"The file has {fileLines.Length} lines.");
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
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 6 years ago.
Improve this question
I have two urls:
http://sp2013/sites/1234
and
http://sp2013/pwa/projectsites/projectdetails.aspx?projuid=1234-123-123-123456-123456
I want to get from the first url the last part (always a number)
1234
and from the second url also the last part (guid)
1234-123-123-123456-123456
How could i achieve this through RegEx or maybe with string operations in C#?
string s = "http://sp2013/sites/1234";
var firstURLlastPart = new Uri(s).Segments.Last();
string s = "http://sp2013/pwa/projectsites/projectdetails.aspx?projuid=1234-123-123-123456-123456";
var secondURLlastPart = s.Split('=').Last();
Just split at '/' and take the last chunk for the first case.
str.Split('/').Last()
Split at '=' and take last chunk for second case
str.Split('=').Last()
(?!\/|\=).[-0-9]+$
It's working for both. But it also accepts if in first example will be number in format like this:
12345-4568
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
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.