如何获取字符串的最后和第一个特殊字符?

时间:2016-11-11 09:58:44

标签: c# arrays regex split

我在列表中输入了一些字符串:

List<string> mylist = new List<string>();
mylist.Add("_+hello[]_");
mylist.Add("_+how||");
mylist.Add("_+are,,");
mylist.Add("||you=_");
mylist.Add("_+doing\"_");

我想在数组中得到最后和第一个特殊字符(+,_ [] =“)。

string[,] array = new string[5,2]

我的数组应该在最后看起来像这样:

_+ AND [] _
_+ AND ||
_+ AND ,,
|| AND =_
_+ AND "_

有谁知道怎么做?

提前致谢

2 个答案:

答案 0 :(得分:3)

您似乎想要使用字母数字字符进行拆分。由于public enum Ethnicity { Acadian, Afghans, Albanians, Arabs, Armenians, Assyrians, Azerbaijanis, Balochis, Bamars, Basques, Bengali, Berbers, Bihari, Bosniaks, Brahui, Bulgarians, Cajun, Catalans, Ceylonese, Cham, Chechens, Cherokees, Chicanos, Chitpavan, Chuvash, Circassians, Congolese, Copts, Croats, Czechs, Danes, Dougla, Dutch, English, Estonians, Eritreans, Faroese, Finns, Flemings, French, Frisians, Gagauz, Galicians, Gerashis, Germans, Greeks, Georgians, Gujarati, Hakka, HanChinese, Hapa, Hindustani, Hmong, Hongkonger, Hui, Hungarians, Icelanders, Igbo, Inuits, Indians, Indochinese, Irish, Istrian, Italians, Japanese, Jassic, Javanese, Jews, Macedonians, Malayali, Kannada, Kazakhs, Khmer, Koreans, Kosovans, Kurds, Kyrgyz, Maghrebis, Malays, Marathi, Moluccan, Norwegians, Laz, Lebanese, Manchu, Marabou, Moldovans, Mongols, Muscogee, Navajo, NEWCaledoniaKanaks, Nepali, Pashtuns, Occitans, Okinawan, Oromo, Persians, Poles, Portuguese, Punjabi, Quebecois, Romanians, Romani, Russians, Saharauis, Scottish, Serbs, Sindhis, Sinhalese, Slovaks, Slovenes, Spaniards, Sundanese, Swedes, Tamils, Tartars, Tejanos, Telugu, Thais, Tibetan, Tuaregs, Turks, Turkmens, Ukrainians, Uyghur, Vietnamese, VolgaTatars, Walloons, Welsh, Yoruba, Zhuang, DoesntMatter; public static Ethnicity getEthnicity(String value) { try { value = URLDecoder.decode(value, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (value.equalsIgnoreCase("Doesn’t Matter") || value.contains("Matter")) { return Ethnicity.DoesntMatter; } else { return Ethnicity.valueOf(value); } } 与字母数字或下划线字符匹配,您可以subtract \w形成它并将其与_一起使用:

Regex.Split

请参阅this regex demo

或者

Regex.Split(str, @"[\w-[_]]+")

这是regex demo

Regex.Split(str, @"[^\W_]+") 匹配任何不是非单词字符的字符(即,它匹配任何[^\W_])而不匹配\w(因此,任何_除了\w否则会匹配)。

答案 1 :(得分:-2)

    List<string> mylist = new List<string>();
    mylist.Add("_+hello[]_");
    mylist.Add("_+how||");
    mylist.Add("_+are,,");
    mylist.Add("||you=_");
    mylist.Add("_+doing\"_");
    foreach (var item in mylist)
    {
        var LastItem=item.ToArray().Last();
        var FirstItem = item.ToArray().First();
    }