简体   繁体   English

如何确定 String.Split() 中使用的字符

[英]How to determine character used in String.Split()

If I am using.Split() how can I find out which character caused the split?如果我使用 .Split() 如何找出导致分裂的字符?

string myMessage = "Apple|Car,Plane-Truck";
//Break apart string
var splits = myMessage.Split(new Char [] {'|', ',', '-'});

foreach (string piece in splits)
{
}

How could I determine which Char ended up being used for the split?我如何确定哪个 Char 最终被用于拆分?

It will split on all of them in the example you've given.在您给出的示例中,它将对所有这些进行拆分。 but in general, you would just see which of the defined split characters are contained in the string:但通常,您只会看到字符串中包含哪些已定义的拆分字符:

var sourceString = "Apple|Car,Plane-Truck";
var allSplitChars = new[] {'|', ',', '-', '.', '!', '?'};

// Find only the characters that are contained in the source string
List<char> charsUsedToSplit = allSplitChars.Where(sourceString.Contains).ToList();
string myMessage = "Apple|Car,Plane-Truck"; //Break apart string var splits = myMessage.Split(new Char[] { '|', ',', '-' }); int accumulated_length = 0; foreach (string piece in splits) { accumulated_length += piece.Length + 1; if (accumulated_length <= myMessage.Length) { Console.WriteLine("{0} was split at {1}", piece, myMessage[accumulated_length - 1]); } else { Console.WriteLine("{0} was the last one", piece); } }

Any characters in the list will be used for the split.. can you clarify what you're actually trying to do?列表中的任何字符都将用于拆分..你能澄清一下你实际上想要做什么吗? in your example the tokens after the split will be "Apple", "Car", "Plane", "Truck" so each of your characters will be used to split..在您的示例中,拆分后的标记将是“Apple”、“Car”、“Plane”、“Truck”,因此您的每个角色都将用于拆分..

If you're trying to determine which character caused the split for each token, then perhaps you might implement the split yourself and keep track:如果您正在尝试确定哪个字符导致每个标记的拆分,那么您可能会自己实现拆分并跟踪:

List<Tuple<String, Char>> Splitter(string msg, char[] chars) {
  var offset = 0;
  var splitChars = new HashSet<char>(chars); 
  var splits = new List<Tuple<String, Char>>();
  for(var idx = 0; idx < msg.Length; idx++) {
    if (splitChars.Contains(msg[idx])) {
      var split = Tuple.Create(msg.Substring(offset, idx - offset), msg[idx]);
      splits.Add(split);
      offset = idx + 1;
    } 
  }
  return splits;
}

string myMessage = "Apple|Car,Plane-Truck";
var splits = Splitter(myMessage, new [] {'|', ',', '-'});
foreach (string piece in splits)
{
    Console.WriteLine("word: {0}, split by: {1}", piece.Item1, piece.Item2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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