Splite 函数

本文介绍了一个自定义的字符串Split方法,该方法可以替代.NET框架中默认的Split方法,并提供了单元测试验证其正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Splite 是用得很多的字符串方法。
自己来实现一下。

ContractedBlock.gifExpandedBlockStart.gifCode
   public static List<string> Splite(string src,char patChar)
        {
            List
<string> result = new List<string>();
            
if (string.IsNullOrEmpty(src))
            {
                
return null;
            }
            
else
            {
                
int endIndex = 0;
                
int frontIndex = -1;
                
while (endIndex<src.Length)
                {
                    
if (src[endIndex] != patChar)
                    {
                       
                    }
                    
else 
                    {
                        
string temp = string.Empty;
                        
if (endIndex - frontIndex - 1 > 0)
                        {
                            temp 
= src.Substring(frontIndex + 1, endIndex - frontIndex-1);
                        }
                            result.Add(temp);
                            frontIndex 
= endIndex;
                        
                    }

                    endIndex
++;
                }

                
if (frontIndex <=src.Length-1)
                {
                    
string temp=string.Empty;
                    
if (frontIndex + 1 <= src.Length - 1)
                    {
                        temp 
= src.Substring(frontIndex + 1);
                    }
                    result.Add(temp);
                }


                
return result;
            }
           
        }

        
public static string[] SpliteArray(string src, char patChar)
        {
           
            List
<string> re= Splite(src,patChar);
            
if (re != null)
            {

                
return re.ToArray();
            }
            
else
            {
                
return null;
            }

        }

单元测试如下:

ContractedBlock.gifExpandedBlockStart.gifCode
/// <summary>
        
///Splite 的测试
        
///</summary>
        [TestMethod()]
        
public void SpliteTest()
        {
            
string[] srcList =
            {
                
"i  love this game",
                
" i  love this game",
                
"  i  love this game",
                
"i  love this game ",
                
"i  love this game  ",
                
"i  love this  game",
                
"i  love  this game   ",
                
"  i  love  this game  ",
                
"   i  love  this game   ",
                
"i  love  this game",
                
"   i  love  this  game   ",
            
            };

            
foreach (string src in srcList)
            {
                
                
string[] expected = src.Split(' ');
                
string[] actual;
                actual 
= StringMagic.SpliteArray(src, ' ');

                
bool equal = true;
                
for (int i = 0; i < expected.Length; i++)
                {
                    
if (expected[i] != actual[i])
                    {
                        equal 
= false;
                        
break;
                    }
                }
                Assert.AreEqual(equal, 
true);
            }
        }

转载于:https://www.cnblogs.com/netfuns/archive/2009/09/12/1565599.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值