简体   繁体   中英

How to get values from textbox to place holder automatically

below is my place holder in which the values are hard coded.

var abc = string.Format($"{123456} {123456} {12345} {123456789012345} {12345678901234567890123} {1234} {1234} {123} {1234567890123} {123456789012345} {1} {123456789012345} 
{123} {12} {12345678901234567890} {1} {1234}");

File.WriteAllText(FilePath + "\\CDR-" + 
                  DateTime.Now.ToString("MM-dd-yyyy HH-mm-ss") + ".txt", abc);

But I want to get values from text box automatically in these placeholders.

Suppose below is my text box and it is the values.

123456 123456 12345 123456789012345 12345678901234567890123 1234 1234 123 1234567890123 123456789012345 1 123456789012345 123 12 12345678901234567890 1 1234.

You can split the input string using the sequence separator (it appears to be a white space here), and use the string array generated by string.Spilt() as the input of the string.Format() method.

Something like this:

string FilePath = @"[Some Path]";
string[] values = textBox1.Text.Split();
var abc = string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16}",
                        values);

File.WriteAllText(Path.Combine(FilePath, "CDR-" + DateTime.Now.ToString("MM-dd-yyyy HH-mm-ss") + ".txt"), abc);

If you have a different separator, specify it as a parameter of the Split() method.

The white space is the predefined character. No need to specify it in this case. With different symbols:

A single character: .Split(',');
More than one: .Split(new[] {',', '+'});
Strings: .Split(new[] { ",", "+" }, StringSplitOptions.RemoveEmptyEntries);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM