当前位置:网站首页>关于字符串转换的一些小技巧
关于字符串转换的一些小技巧
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;
}
边栏推荐
- If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience
- 自定义标题栏View
- Bisection (integer bisection and floating point bisection)
- MapReduce project case 1
- 深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...
- fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
- Remote login sshd service
- What is data compliance? How to achieve data compliance?
- AcWing 610. Salary and bonus (implemented in C language)
- fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
猜你喜欢

Day32 JS note event (Part 1) September 27, 2021

In less than an hour, apple destroyed 15 startups

Custom title bar view

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

.NET混合开发解决方案24 WebView2对比CefSharp的超强优势

【C语言】判断三角形

多维度监控:智能监控的数据基础

Function and principle of remoteviews

Redis 原理 - List

Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
随机推荐
Django -- MySQL database reflects the mapping data model to models
双缓冲绘图
Multi dimensional monitoring: the data base of intelligent monitoring
On the output representation of bidirectional LSTM in pytoch
【C语言】文件读写函数使用
Use logrotate to automatically cut the website logs of the pagoda
URL append parameter method, considering #$ Situation of
【C语言】随机数文件对其进行三种排序方法
ByteV搭建动态数字孪生网络安全平台----助力网络安全发展
AcWing 608. Poor (implemented in C language)
Random forest and poetry maker trained by AMR
JS foundation 8
AcWing 605. Simple product (implemented in C language)
Using MySQL database in the express framework of node
PrecomputedTextCompat用法及原理
AcWing 606. Average 1 (implemented in C language)
AcWing 610. Salary and bonus (implemented in C language)
recent developments
What method is required for word, PDF and txt files to realize full-text content retrieval?
The default point of this in JS and how to modify it to 2021.11.09