将数组传递给函数(并使用函数拆分数组)

发布于 2024-11-04 02:43:19 字数 777 浏览 3 评论 0原文

我想传递一个字符串数组(以逗号分隔),然后使用一个函数用逗号分隔传递的数组,并添加分隔符来代替逗号。

我将通过一些损坏的代码更详细地向您展示我的意思:

String FirstData = "1";
String SecondData = "2" ;
String ThirdData = "3" ;
String FourthData = null;

FourthData = AddDelimiter(FirstData,SecondData,ThirdData);

public String AddDelimiter(String[] sData)        
{
    // foreach ","
    String OriginalData = null;

    // So, here ... I want to somehow split 'sData' by a ",". 
    // I know I can use the split function - which I'm having 
    // some trouble with - but I also believe there is some way
    // to use the 'foreach' function? I wish i could put together 
    // some more code here but I'm a VB6 guy, and the syntax here 
    // is killing me. Errors everywhere.

    return OriginalData;

}

I want to pass a string array (separated by commas), then use a function to split the passed array by a comma, and add in a delimiter in place of the comma.

I will show you what I mean in further detail with some broken code:

String FirstData = "1";
String SecondData = "2" ;
String ThirdData = "3" ;
String FourthData = null;

FourthData = AddDelimiter(FirstData,SecondData,ThirdData);

public String AddDelimiter(String[] sData)        
{
    // foreach ","
    String OriginalData = null;

    // So, here ... I want to somehow split 'sData' by a ",". 
    // I know I can use the split function - which I'm having 
    // some trouble with - but I also believe there is some way
    // to use the 'foreach' function? I wish i could put together 
    // some more code here but I'm a VB6 guy, and the syntax here 
    // is killing me. Errors everywhere.

    return OriginalData;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

堇年纸鸢 2024-11-11 02:43:19

语法在这里并不重要,您需要了解基类库。另外,你显然想要连接字符串,而不是分割它:

var s = string.Join(",", arrayOFStrings);

另外,如果你想将 n 个字符串传递给这样的方法,你需要 params 关键字:

public string Join( params string[] data) {
  return string.Join(",", data);
}

Syntax doesn't matter much here, you need to get to know the Base Class Library. Also, you want to join strings apparently, not split it:

var s = string.Join(",", arrayOFStrings);

Also, if you want to pass n string to a method like that, you need the params keyword:

public string Join( params string[] data) {
  return string.Join(",", data);
}
无语# 2024-11-11 02:43:19

拆分:

string[] splitString = sData.Split(new char[] {','});

要加入新的分隔符,请将字符串数组传递给 String.Join

string colonString = String.Join(":", splitString);

我认为您最好使用 Replace,因为您要做的就是将一个分隔符替换为另一个:

string differentDelimiter = sData.Replace(",", ":");

To split:

string[] splitString = sData.Split(new char[] {','});

To join in new delimiter, pass in the array of strings to String.Join:

string colonString = String.Join(":", splitString);

I think you are better off using Replace, since all you want to do is replace one delimiter with another:

string differentDelimiter = sData.Replace(",", ":");
你与清晨阳光 2024-11-11 02:43:19

如果您有多个对象并且想要将它们放入数组中,您可以这样写:

string[] allData = new string[] { FirstData, SecondData, ThirdData };

然后您可以简单地将其传递给函数:

FourthData = AddDelimiter(allData);

C# 有一个很好的技巧,如果您将 params 关键字添加到函数定义,您可以将其视为具有任意数量参数的函数:

public String AddDelimiter(params String[] sData) { … }
…
FourthData = AddDelimiter(FirstData, SecondData, ThirdData);

至于实际实现,最简单的方法是使用 string.Join()

public String AddDelimiter(String[] sData)        
{
    // you can use any other string instead of ":"
    return string.Join(":", sData); 
}

但是如果您想自己构建结果(例如,如果您想学习如何做到这一点),您可以这样做使用字符串连接 (oneString + anotherString),或者更好的是使用 StringBuilder

public String AddDelimiter(String[] sData)        
{
    StringBuilder result = new StringBuilder();
    bool first = true;

    foreach (string s in sData)
    {
        if (!first)
            result.Append(':');
        result.Append(s);
        first = false;
    }

    return result.ToString();
}

If you have several objects and you want to put them in an array, you can write:

string[] allData = new string[] { FirstData, SecondData, ThirdData };

you can then simply give that to the function:

FourthData = AddDelimiter(allData);

C# has a nice trick, if you add a params keyword to the function definition, you can treat it as if it's a function with any number of parameters:

public String AddDelimiter(params String[] sData) { … }
…
FourthData = AddDelimiter(FirstData, SecondData, ThirdData);

As for the actual implementation, the easiest way is to use string.Join():

public String AddDelimiter(String[] sData)        
{
    // you can use any other string instead of ":"
    return string.Join(":", sData); 
}

But if you wanted to build the result yourself (for example if you wanted to learn how to do it), you could do it using string concatenation (oneString + anotherString), or even better, using StringBuilder:

public String AddDelimiter(String[] sData)        
{
    StringBuilder result = new StringBuilder();
    bool first = true;

    foreach (string s in sData)
    {
        if (!first)
            result.Append(':');
        result.Append(s);
        first = false;
    }

    return result.ToString();
}
愁杀 2024-11-11 02:43:19

Split 函数的一个版本采用字符数组。这是一个例子:

   string splitstuff = string.Split(sData[0],new char [] {','});

One version of the Split function takes an array of characters. Here is an example:

   string splitstuff = string.Split(sData[0],new char [] {','});
花间憩 2024-11-11 02:43:19

如果您不需要对中间的部分执行任何处理而只需要替换分隔符,则可以使用 String 类上的Replace 方法

string newlyDelimited = oldString.Replace(',', ':');

对于大字符串,这将为您提供更好的性能,因为您赢了不必完全穿过绳子将其分开,然后穿过各个部分将它们重新连接在一起。

但是,如果您需要使用各个部分(将它们重新组合成另一种形式,而不是简单地替换分隔符),那么您将使用 String 上的Split 方法来获取分隔项的数组,然后将它们插入您想要的格式。

当然,这意味着您必须对分隔字符串的每个部分的含义有某种明确的了解。

If you don't need to perform any processing on the parts in between and just need to replace the delimiter, you could easily do so with the Replace method on the String class:

string newlyDelimited = oldString.Replace(',', ':');

For large strings, this will give you better performance, as you won't have to do a full pass through the string to break it apart and then do a pass through the parts to join them back together.

However, if you need to work with the individual parts (to recompose them into another form that does not resemble a simple replacement of the delimiter), then you would use the Split method on the String class to get an array of the delimited items and then plug those into the format you wish.

Of course, this means you have to have some sort of explicit knowledge about what each part of the delimited string means.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文