使用分隔符Xamarin Forms分割和格式化文本输出

时间:2018-08-26 14:32:31

标签: xamarin xamarin.forms

我有一个条目和标签,我想像这样将文本格式设置为标签:

  

“ email@gmail.com”,“ email2@gmail.com”,“ email3@gmail.com”

这是我在输入字段中输入的内容:

  

email@gmail.com / space / email2@gmail.com / space / email3@gmail.com



  email @ gmail.com,email2 @ gmail.com,email3 @ gmail.com

分隔符是空格或逗号。如何将输出格式化为上面的输出?

2 个答案:

答案 0 :(得分:1)

好问题!

string entry = Entry.Text;
List<string> arrayfromEntry = new List<string>();
          if (entry.Contains(" ") == true){
          arrayfromEntry = entry.Split(new char[] { ' ' }).ToList();
          }
          else{
            arrayfromEntry = entry.Split(new char[] { ',' }).ToList();
          }
          for (int i = 0; i < arrayfromEntry.Count(); i++){
            arrayfromEntry[i] = '"' + arrayfromEntry[i] + '"'; 
          }
string f = (string.Join(", ",arrayfromEntry));
f = f.Remove(f.Count()-2,2);
f = f+'"';
textToLabel = f;

Entry.Text是您输入内容中的文本,而textToLabel则是更改标签文本的地方,

答案 1 :(得分:1)

基于@jamesfdearborn答案,但改用StringBuilder

string entry == "aaaa@ttttt.com,bbbb@ttttttyyy.com,tttt@errrer.com,yyyyyy@rrrttr.com,uuuuu@yuyuy.com";
var inputSeparator = ','; //comma is the separator in this case you can change it 
var outputSeparator = ',';

var arrayfromEntry = entry.Split(inputSeparator).ToList();

var sb = new StringBuilder();

for (int i = 0; i < arrayfromEntry.Count(); i++)
{
    sb.AppendFormat("\"{0}\"{1}",arrayfromEntry[i],outputSeparator);    
}

sb.Remove(sb.ToString().Count()-1, 1);

sb.ToString() //result here 
//output
//"aaaa@ttttt.com","bbbb@ttttttyyy.com","tttt@errrer.com","yyyyyy@rrrttr.com","uuuuu@yuyuy.com"

您可以更改输出或输入分隔符