当前位置:网站首页>关于字符串转换的一些小技巧
关于字符串转换的一些小技巧
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;
}
边栏推荐
- RemoteViews的作用及原理
- Custom title bar view
- URL append parameter method, considering #$ Situation of
- Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
- Still using simpledateformat for time formatting? Be careful that the project collapses!
- Day32 JS note event (Part 1) September 27, 2021
- Why do many people want to change careers as programmers, while some programmers want to change careers as others?
- MapReduce project case 3 - temperature statistics
- What method is required for word, PDF and txt files to realize full-text content retrieval?
- AcWing 607. Average 2 (implemented in C language)
猜你喜欢

Day36 JS notes ecma6 syntax 2021.10.09

Simulation of the Saier lottery to seek expectation

JS foundation 8

Self use demo of basic component integration of fluent

If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience

Share the easy-to-use fastadmin open source system - practical part

什么是数据合规?怎样做到数据合规?

【Unity编辑器扩展基础】、EditorGUILayout(二)

Using soapUI to obtain freemaker's FTL file template

开源项目维权成功案例: spug 开源运维平台成功维权
随机推荐
2018 joint examination of nine provinces & Merging of line segment trees
In less than an hour, apple destroyed 15 startups
智联招聘基于 Nebula Graph 的推荐实践分享
Remoteviews layout and type restriction source code analysis
5. Sum of N numbers
Use logrotate to automatically cut the website logs of the pagoda
Convert black mask picture to color annotation file
【C语言】如何很好的实现复数类型
双缓冲绘图
【Unity编辑器扩展基础】、EditorGUILayout(二)
Redis principle - List
Leetcode 705. 设计哈希集合
Solutions to connection failures and errors when accessing mysql8 using the SSM project
开源项目维权成功案例: spug 开源运维平台成功维权
Custom title bar view
Two writing methods of JNI function
Leetcode 48. 旋转图像(可以,已解决)
Function and principle of remoteviews
How to deploy the software testing environment?
Intranet penetration in the working group environment: some basic methods