如何在不使用.Split()的情况下将字符串拆分为字符串数组 [英] How to split A string into Array of Strings without using .Split()

查看:128
本文介绍了如何在不使用.Split()的情况下将字符串拆分为字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hye

我想将字符串拆分为一个字符串数组但我必须不使用string.Split()方法c#提供



任何可以帮助我的人? thanx

hye
I want to split the string into An array of string but i have to do it without using string.Split() method c# provides

Anyone who could help me? thanx

推荐答案

好吧,作为解决方案,我建议您查看以下链接:



MSDN字符串类定义 [ ^ ]

在那里,看看方法:



IndexOf,SubString,替换。



有了它们你就可以轻松运行一些for循环并复制split功能。



给你的作业一个去看看你是怎么过的。
Well, as a solution I suggest you check out the following link:

MSDN String Class Definition[^]
On there, Look at the methods:

IndexOf, SubString, Replace.

With them you could easily run some for loop and replicate the "split" function.

Give your homework a go and see how you get on.


你可以把你自己的分割方法作为



You can crate your own split method as

public String[] SplitText(String txt, char[] delim)
        {
            if (txt == null)
                return new String[0]; // or exception
            if (delim == null || delim.Length == 0)
                return new String[0]; // or exception

            char[] text = txt.ToCharArray();
            string[] result = new string[1]; // If there is no delimiter in the string, return the whole string
            int part = 0;
            int itemInArray = 1;

            for (int i = 0; i < text.Length; i++)
            {
                if (IsIn(delim, text[i]))
                {
                    Array.Resize(ref result, ++itemInArray); // Is it consider as a framework method ???
                    part++;
                }
                else
                    result[part] += text[i];
            }
            return result;
        }
        public static Boolean IsIn(char[] delim, char c)
        {
            for (int i = 0; i < delim.Length; i++)
                if (c == delim[i])
                    return true;
            return false;
        }







然后像这样叫它








and then call it like this


char[] delimter = new char[1];
delimter[0] = '-';
string[] b = SplitText("Your-string-goes-here", delimter);


这篇关于如何在不使用.Split()的情况下将字符串拆分为字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆