最近整理了一些C#常用的字符串处理方法,大致如下:
1--用replace删除特定字符
2--从第i个字符提取长度为j的字符
3--提取字符串中最右边长度为i的字符串
4--替换字符串中的特定字符串
5--删除指定位置(i)的指定长度的字符串
6--单个分隔字符用split截取
7--多个分隔字符用split截取
8--多个分隔字符串用split截取
9--查找一个字符串在指定字符串中的位置
计算字符串中有多少个指定字符串
10--在字符串指定位置添加另外一字符串
11--十六进制、整数相互转换
以上是整理的一些常用字符串处理方法,后续将继续补充!
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StrTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n\ntest 11--十六进制、整数相互转换");
char ch11_a = 'a';//97
char ch11_A = 'A';//65
char ch11_0 = '0';//48
Console.WriteLine("char a:{0}\nchar A:{1}\nchar 0:{2}", (int)ch11_a, (int)ch11_A,(int)ch11_0);
Console.WriteLine("A1->{0} \t3C->{1} \t0111->{2}",Hex2Int_U("A1"),Hex2Int_U("3C"),Hex2Int_U("0111"));
Console.WriteLine("161->{0} \t60->{1} \t273->{2}",Int_U2Hex(161),Int_U2Hex(60),Int_U2Hex(273));
//更简单的int2hex方法
Console.WriteLine("161->{0} \t60->{1}",Convert.ToString(161,16).ToUpper(),Convert.ToString(60,16).ToUpper());
Console.WriteLine("\n\ntest 10--在字符串指定位置添加另外一字符串");
string str10 = "B2";
Console.WriteLine("Before insert:"+str10+",len:{0}",str10.Length);
str10 = str10.Insert(0,"A1");
Console.WriteLine("After insert:"+str10+",len:{0}",str10.Length);
Console.WriteLine("\n\ntest 9--查找一个字符串在指定字符串中的位置");
string str9 = "A5010200003C01A5010200003C02A5010200003C03A5010200003C04";
Console.WriteLine(str9 + ",len:" + str9.Length);
int l9_1 = str9.IndexOf("A5");
Console.WriteLine("第1个 A5 loaction:" + l9_1);
int l9_2 = str9.IndexOf("A5",l9_1+2,14);//从第l9+1位置开始,在之后的14个字节中A5的位置;若改为15则在l9+2到l9+13中没有A5,所以返回为-1
Console.WriteLine("第2个 A5 loaction:" + l9_2);
int l9_3 = str9.IndexOf("A5", l9_2 + 2);
Console.WriteLine("第3个 A5 loaction:" + l9_3);
int l9_last = str9.LastIndexOf("A5");
Console.WriteLine("第4个 A5 loaction:" + l9_last);
Console.WriteLine("\n--计算字符串中有多少个指定字符串");
string[] sArray = str9.Split(new string[] { "A5" }, StringSplitOptions.None); //StringSplitOptions.RemoveEmptyEntries 不包括空值
int A5_num = sArray.Length-1;//将字符串以A5分段,若A5在头或尾则会有一个空字符串
Console.WriteLine("A5 num:{0}",A5_num);
Console.WriteLine("\n\ntest 8--多个分隔字符串用split截取");
string str8 = "A1--B233C3--D433E5";
Console.WriteLine(str8 + ",len:" + str8.Length);
string[] sArray8 = str8.Split(new string[] { "--", "33" }, StringSplitOptions.RemoveEmptyEntries);//sArray8.Length 计算字符串数组的个数
foreach (string str in sArray8)
{
Console.Write(str+"\t");
}
Console.WriteLine("\n\n\ntest 7--多个分隔字符用split截取");
string str7 = "A1_B2,C3_D4,E5";
Console.WriteLine(str7 + ",len:" + str7.Length);
string[] sArray7 = str7.Split('_', ',');//等价于 str7.Split(new char[2]{',','_'});
foreach (string str in sArray7)
{
Console.Write(str+"\t");
}
Console.WriteLine("\n\n\ntest 6--单个分隔字符用split截取");
string str6 = "A1_B2_C3_D4";
Console.WriteLine(str6+",len:"+str6.Length);
string[] sArray6 = str6.Split('_');
foreach(string str in sArray6)
{
Console.Write(str+"\t");
}
Console.WriteLine("\n\n\ntest 5--删除指定位置(i)的指定长度的字符串:");
string str5 = "GTAZB_JiangjBen_123";
Console.WriteLine(str5+",leng:"+str5.Length);
str5 = str5.Remove(0, 5);//i=0 即为从第一个字符开始删除
Console.WriteLine( str5+",len:" + str5.Length);
Console.WriteLine("\n\ntest 4--替换字符串中的特定字符串:");
string str4 = "GTAZB_JiangjBen_123";
Console.WriteLine(str4);
str4 = str4.Replace("123", "321");
Console.WriteLine(str4 + ",len:{0}", str4.Length);
Console.WriteLine("\n\ntest 3--提取字符串中最右边长度为i的字符串:");
//获取最左边n个字符str3.Substring(0,n);
//获取最右边n个字符str3.Substring(str3.Length - n);
string str3 = "GTAZB_JiangjBen_123";
Console.WriteLine(str3);
str3 = str3.Substring(str3.Length - 8);
Console.WriteLine(str3 + ",len:{0}", str3.Length);
Console.WriteLine("\n\ntest 2--从第i个字符提取长度为j的字符:");
string str2 = "GTAZB_JiangjBen_123";
Console.WriteLine(str2);
str2 = str2.Substring(3 - 1, 8);
Console.WriteLine(str2 + ",len:{0}", str2.Length);
Console.WriteLine("\n\ntest 1--用replace删除特定字符:");
string str1 = "GTAZB_JiangjBen_123";
Console.WriteLine(str1 + ",len:{0}", str1.Length);
str1 = str1.Replace("Jiang","");
Console.WriteLine(str1 + ",len:{0}", str1.Length);
Console.WriteLine();
}
#region 十六进制、整数互转
public static long Hex2Int_U(string hex)
{
//Hex2Int_U 表示转换为无符号整数
//long 最长为64位有符号整数
//0xaa为1字节,则最多可以解析8字节的数据
//实际为了保证为无符号的,只解析前7个(14个字符)十六进制数据,若小于14字符且为单数的前面默认补一个0
long ret = 0;
string hex0 = hex;
int[] hex_int = new int[14];
int len = hex0.Length;
if (len > 0 && len < 14)
{
if (len % 2 == 1)
{
hex0 = hex0.Insert(0, "0");
len += 1;
}
}
else if(len>14)
{
len = 14; ;
}
for (int i = 0; i < len;i++ )
{
hex_int[i] = Char2Int(hex0[i]);
}
for (int i = 0; i < len; i++)
{
ret += (long)hex_int[i] * ((long)Math.Pow(16, len-i-1));//Math.Pow(x,n) = x^n
}
return ret;
}
public static string Int_U2Hex(long num_int)
{
//Hex2Int_U 表示转换为无符号整数
//long 最长为64位有符号整数
//0xaa为1字节,则最多可以生成8字节的数据
//实际为了保证为无符号的,只生成7个(14个字符)十六进制数据,若字符为单数则在前面默认补一个0
string ret = "";
int[] myint = new int[16];
long num = num_int;
int i = 0;
while(num>0)
{
myint[i] = (int)(num % 16);
num = num / 16;
i++;
}
for (int j = i-1; j >=0; j--)
{
ret += Int2Char(myint[j]);
}
if (i % 2 == 1)
{
ret = ret.Insert(0,"0");
}
return ret;
}
public static int Char2Int(char ch)
{
int ret = 0;
if (ch >= '0' && ch <= '9')
{
ret = ch - '0';
}
else if (ch >= 'a' && ch <= 'f')
{
ret = ch - 'a' + 10;
}
else if (ch >= 'A' && ch <= 'F')
{
ret = ch - 'A' + 10;
}
else
{
;
}
return ret;
}
public static char Int2Char(int num_int)
{
char ret = '0';
if(num_int>=0 && num_int<=9)
{
ret = (char)((int)'0' + num_int);
}
else if (num_int >= 10 && num_int <= 15)
{
ret = (char)((int)'A' + num_int -10);
}
else
{
ret = '0';
}
return ret;
}
#endregion
}
}