将字符串转换为三个字母的缩写
Convert string into three letter Abbreviation
最近工作给我一个新项目,将任何给定的字符串转换为 1-3 个字母的缩写。
下面是一个类似于我必须生成的内容的示例,但是给出的字符串可以是任何内容:
switch (string.Name)
{
case "Emotional, Social & Personal": return "ESP";
case "Speech & Language": return "SL";
case "Physical Development": return "PD";
case "Understanding the World": return "UW";
case "English": return "E";
case "Expressive Art & Design": return "EAD";
case "Science": return "S";
case "Understanding The World And It's People"; return "UTW";
}
我想我可以使用 string.Split 并计算数组中的单词数。然后添加处理特定长度字符串的条件,因为通常这些句子不会超过 4 个单词,但我会遇到的问题是。
- 如果一个字符串比我预期的要长,它就不会被处理
- 必须从缩写中排除符号
任何关于我可以应用的逻辑的建议都将不胜感激。
谢谢
看来要是无所谓的话,就拿最简单的吧。如果字符串短于 4 个单词,则取每个字符串的第一个字母。
如果字符串长于4,则将"ands"和"ors"全部剔除,然后同理
为了更好,您可以查找您不关心的单词的字典 - 例如 "the" 或 "so"。
您还可以保留一个 3D 字符数组,按字母顺序排列以便快速查找。这样,您就不会有任何重复的缩写。
但是,缩写的数量是有限的。因此,最好将 'useless' 单词存储在另一个字符串中。这样,如果您的程序默认使用的缩写已被使用,您可以使用无用的单词创建一个新的。
如果以上所有方法都失败,您可以开始在字符串中线性移动以获得不同的 3 字母单词缩写 - 有点像 DNA 上的密码子。
类似下面的内容应该适用于您提供的示例。
string abbreviation = new string(
input.Split()
.Where(s => s.Length > 0 && char.IsLetter(s[0]) && char.IsUpper(s[0]))
.Take(3)
.Select(s => s[0])
.ToArray());
您可能需要根据您的预期输入调整过滤器。可能会添加要忽略的单词列表。
使用字典的完美场所
Dictionary<string, string> dict = new Dictionary<string, string>() {
{"Emotional, Social & Personal", "ESP"},
{"Speech & Language","SL"},
{"Physical Development", "PD"},
{"Understanding the World","UW"},
{"English","E"},
{"Expressive Art & Design","EAD"},
{"Science","S"},
{"Understanding The World And It's People","UTW"}
};
string results = dict["English"];
以下代码片段可能对您有所帮助:
string input = "Emotional, Social & Personal"; // an example from the question
string plainText = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Regex.Replace(input, @"[^0-9A-Za-z ,]", "").ToLower()); // will produce a text without special charactors
string abbreviation = String.Join("",plainText.Split(" ".ToCharArray(),StringSplitOptions.RemoveEmptyEntries).Select(y =>y[0]).ToArray());// get first character from each word
最近工作给我一个新项目,将任何给定的字符串转换为 1-3 个字母的缩写。 下面是一个类似于我必须生成的内容的示例,但是给出的字符串可以是任何内容:
switch (string.Name)
{
case "Emotional, Social & Personal": return "ESP";
case "Speech & Language": return "SL";
case "Physical Development": return "PD";
case "Understanding the World": return "UW";
case "English": return "E";
case "Expressive Art & Design": return "EAD";
case "Science": return "S";
case "Understanding The World And It's People"; return "UTW";
}
我想我可以使用 string.Split 并计算数组中的单词数。然后添加处理特定长度字符串的条件,因为通常这些句子不会超过 4 个单词,但我会遇到的问题是。
- 如果一个字符串比我预期的要长,它就不会被处理
- 必须从缩写中排除符号
任何关于我可以应用的逻辑的建议都将不胜感激。 谢谢
看来要是无所谓的话,就拿最简单的吧。如果字符串短于 4 个单词,则取每个字符串的第一个字母。 如果字符串长于4,则将"ands"和"ors"全部剔除,然后同理
为了更好,您可以查找您不关心的单词的字典 - 例如 "the" 或 "so"。
您还可以保留一个 3D 字符数组,按字母顺序排列以便快速查找。这样,您就不会有任何重复的缩写。
但是,缩写的数量是有限的。因此,最好将 'useless' 单词存储在另一个字符串中。这样,如果您的程序默认使用的缩写已被使用,您可以使用无用的单词创建一个新的。
如果以上所有方法都失败,您可以开始在字符串中线性移动以获得不同的 3 字母单词缩写 - 有点像 DNA 上的密码子。
类似下面的内容应该适用于您提供的示例。
string abbreviation = new string(
input.Split()
.Where(s => s.Length > 0 && char.IsLetter(s[0]) && char.IsUpper(s[0]))
.Take(3)
.Select(s => s[0])
.ToArray());
您可能需要根据您的预期输入调整过滤器。可能会添加要忽略的单词列表。
使用字典的完美场所
Dictionary<string, string> dict = new Dictionary<string, string>() {
{"Emotional, Social & Personal", "ESP"},
{"Speech & Language","SL"},
{"Physical Development", "PD"},
{"Understanding the World","UW"},
{"English","E"},
{"Expressive Art & Design","EAD"},
{"Science","S"},
{"Understanding The World And It's People","UTW"}
};
string results = dict["English"];
以下代码片段可能对您有所帮助:
string input = "Emotional, Social & Personal"; // an example from the question
string plainText = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Regex.Replace(input, @"[^0-9A-Za-z ,]", "").ToLower()); // will produce a text without special charactors
string abbreviation = String.Join("",plainText.Split(" ".ToCharArray(),StringSplitOptions.RemoveEmptyEntries).Select(y =>y[0]).ToArray());// get first character from each word