String array to notepad

I have a string array in C # and I intend to copy it into notepad so that each line is on a string. What should I do?

0


a source to share


6 answers


You can create one line with each element on separate lines, for example:

string[] array = { "a", "b", "c" };
string lines = string.Join(Environment.NewLine, array);

      



Then you can use the Clipboard class to copy the string to the clipboard.

+7


a source


You can use the System.Windows.Forms.Clipboard.SetText method to store text on the clipboard. It will then be available for other applications like notepad to paste.



+2


a source


Maybe something like this? (Depends on what you mean by copy in notebook)

string output = "";

foreach(string s in yourArray)
{
output += s + "\n";
}

      

(Combine this with what Runa said, above ... or below, depending on the rating)

+2


a source


Take a look at

If I understand you correctly, you just want to write to a text file.

  • You will need to iterate over your string array and then write the contents of each index to the string array on a new line in the file.

  • Remember to properly remove the writer when finished.

If you need more information, please let me know and I'll post an example, but I think it's worth getting involved first and see how you do it :)

0


a source


string filepath = "some_path_in_here";
string filename = "test.txt";
StreamWriter sw = new StreamWriter(fileName, false);

for(int i = 0; i < strings_array.Count; i++)
{
sw.Write(strings_array[i]);
sw.Write(sw.NewLine);
}

      

try this, but you better read some book to do simple things like this. Don't expect others to give you an exact solution.

0


a source


You can use StringBuilder to concatenate strings to each other (if you just want to put each string on a new line):

StringBuilder sb=new StringBuilder();
foreach(string line in lines)
{
sb.AppendLine(line);
}

return sb.ToString();

      

Since the string is an immutable object (if it changed a new instance will be created), it is not recommended to do something like:

line=line+"\n\r";

      

0


a source







All Articles