用C#将一串字符串中大写字母转化为小写字母,小写字母换位大写字母,其余字符不变,谢啦

比如输入ASDfghJ,输出asdfGHj
2024-11-09 04:47:10
推荐回答(5个)
回答1:

Public string str(string strs)
{
string str=strs;
char[] ch = str.ToCharArray();
System.Text.RegularExpressions.Regex R = new System.Text.RegularExpressions.Regex("[A-Z]");
str = "";
foreach (char s in ch)
{
if (R.IsMatch(s.ToString()))
{
str += s.ToString().ToLower();
}
else
{
str += s.ToString().ToUpper();
}

}
return str;
}

已经验证,思路液段,把字符转化成 字符串数组,然后遍历,根据团运正则判闹或誉断大小写

回答2:

也可以转换烂核成ascii码再判断

string str = "abc123123按时efff打蚂银算饥物掘DhhHHGjj";
StringBuilder sb = new StringBuilder();
str.ToList().ForEach(a => sb.Append(((short)a >= 65 && (short)a <= 90) ? ((char)((short)a + 32)).ToString() : (((short)a >= 97 && (short)a <= 122)) ? ((char)((short)a - 32)).ToString() : a.ToString()));
MessageBox.Show(sb.ToString());

回答3:

private void button2_Click(object sender, EventArgs e)
{
string stringA = "ASDfghJ";
string str = UpLowerConvert(stringA);
MessageBox.Show(str);
}
///
/// 大小写互转
///

/// <告首param name="chsstr">
///
public static string UpLowerConvert(string chsstr)
{
string strRet = string.Empty;
char[] ArrChar = chsstr.ToCharArray();
foreach (char c in ArrChar)
{
strRet += SingleChs2UpLower(c.ToString());
}
return strRet;
}
///
/// 单个袜昌数字母互转迅局
///

///
///
public static string SingleChs2UpLower(string SingleChs)
{
string strRtn = "";
//将字母转为大/小写
if (Regex.IsMatch(SingleChs, "[A-Z]"))
{
strRtn = SingleChs.ToLower();
}
else if (Regex.IsMatch(SingleChs, "[a-z]"))
{
strRtn = SingleChs.ToUpper();
}
return strRtn;
}

回答4:

string str="ABCabc";
MatchCollection MC = Regex.Matches(str, "[a-z]");
MatchCollection MC2 = Regex.Matches(str, "[A-Z]");
Encoding encode = Encoding.Unicode;
byte[] SelectedBuffer = encode.GetBytes(str);
foreach (Match M in MC)
{
byte[] UBuffer = encode.GetBytes(M.Value.ToUpper());
Array.Copy(UBuffer, 0, SelectedBuffer, M.Index << 1, UBuffer.Length);
}
foreach (Match M in MC2)
{
byte[] UBuffer = encode.GetBytes(M.Value.ToLower());
Array.Copy(UBuffer, 0, SelectedBuffer, M.Index << 1, UBuffer.Length);
}
str = encode.GetString(SelectedBuffer);

回答5:

string temp = null;
string str = "aAbfdFCdfKF";
char[] lowChars = str.ToLower().ToCharArray();
char[] upChars = str.ToUpper().ToCharArray();
for (int i = 0; i < str.Length; i++)
{
temp += str[i] == lowChars[i] ? upChars[i] : lowChars[i];
}