当前位置:网站首页>关于字符串转换的一些小技巧
关于字符串转换的一些小技巧
2022-06-28 12:00:00 【Unique_849997563】
开发中一般都会遇到需要切割字符串的时候,你可以用一些特殊符号作为字符串的分割符,比如+、&、|等,然后用这些符号切割字符串,得到字符串数组,例如:
//string str= "5+30";
public string[] Str2StrArray(string str, char symbol)
{
if (string.IsNullOrEmpty(str))
{
return new string[0];
}
return str.Split(symbol);
}有可能你想要的是Int数组,你可以也可以通过将字符串转换成Int,然后得到Int数组,例如:
//string str= "5+30";
public int[] Str2IntArray(string str, char symbol)
{
if (string.IsNullOrEmpty(str))
{
return new int[0];
}
string[] strArray = str.Split(symbol);
if (strArray.Length < 1)
{
return new int[0];
}
int[] value = new int[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
value[i] = Convert.ToInt32(strArray[i]);
}
return value;
}如果一维数组都不能满足你的需求,你可以多添加两种分割字符,将分割后的数据转成 交叉数组,例如:
public int[][] GetIntArray()
{
string value = "5+30|10+40|15+45|20+50";
string[] _strArry = Str2StrArray(value, '|');
int[][] _temp = new int[_strArry.Length][];
for (int i = 0; i < _strArry.Length; i++)
{
int[] _intArray = Str2IntArray(_strArry[i], '+');
_temp[i] = _intArray;
}
return _temp;
}
边栏推荐
- 【北京航空航天大学】考研初试复试资料分享
- 【C语言】如何很好的实现复数类型
- AcWing 609. Salary (implemented in C language)
- Day29 JS notes 2021.09.23
- 自定义标题栏View
- Day32 JS note event (Part 1) September 27, 2021
- Many benefits of SEO optimization are directly related to traffic
- Dongyuhui, New Oriental and Phoenix Satellite TV
- [Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
- 4. maximum continuity factor
猜你喜欢

Day37 JS note motion function 2021.10.11

Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released

Leetcode 705. 设计哈希集合

内部振荡器、无源晶振、有源晶振有什么区别?

【vi/vim】基本使用及命令汇总

JNI confusion of Android Application Security

已知两个点和中间一个比例的点,求该点坐标

Django -- MySQL database reflects the mapping data model to models

What method is required for word, PDF and txt files to realize full-text content retrieval?

什么是数据合规?怎样做到数据合规?
随机推荐
来吧元宇宙,果然这热度一时半会儿过不去了
【北京航空航天大学】考研初试复试资料分享
Share the easy-to-use fastadmin open source system - practical part
Apache2 configuration denies access to the directory, but can access the settings of the files inside
Self use demo of basic component integration of fluent
Build your own website (18)
Simple understanding of ThreadLocal
Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
ProCAST finite element casting process simulation software
什么是数据合规?怎样做到数据合规?
Daily practice of C language - day 4: find the sum of all even numbers within 100
Usage and principle of precomputedtextcompat
ByteV搭建动态数字孪生网络安全平台----助力网络安全发展
Vivo手机的权限管理
JNI confusion of Android Application Security
2. single digit statistics
KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
Day36 JS notes ecma6 syntax 2021.10.09
Leetcode 48. 旋转图像(可以,已解决)