亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從字符串中刪除 X 個大寫字母?

如何從字符串中刪除 X 個大寫字母?

C#
RISEBY 2022-01-16 20:06:40
我想從 a中刪除X多個大寫字母string。例如,如果我有字符串:string Line1 = "NICEWEather";和string Line2 = "HAPpyhour";我將如何創建一個提取 2 組大寫字母的函數?
查看完整描述

2 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

嘗試對 Unicode大寫字母使用正則表達式\p{Lu}


  using System.Text.RegularExpressions;


  ...


  // Let's remove 2 or more consequent capital letters

  int X = 2;


  // English and Russian

  string source = "NICEWEather - ХОРОшая ПОГОда - Keep It (HAPpyhour)";


  // ather - шая да - Keep It (pyhour)

  string result = Regex.Replace(source, @"\p{Lu}{" + X.ToString() + ",}", "");

這里我們使用\p{Lu}{2,}模式:大寫字母出現X(2在上面的代碼中)或更多次。


查看完整回答
反對 回復 2022-01-16
?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

從字符串中刪除大寫字母


    string str = " NICEWEather";

    Regex pattern = new Regex("[^a-z]");

    string result = pattern.Replace(str, "");

    Console.WriteLine(result );

輸出: ather


如果按順序多次出現,則刪除大寫字母,然后試試這個


string str = " NICEWEather";

Regex pattern = new Regex(@"\p{Lu}{2,}");

string output = pattern.Replace(str, "");

Console.WriteLine(output);


查看完整回答
反對 回復 2022-01-16
  • 2 回答
  • 0 關注
  • 226 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號