将数组传递给函数(并使用函数拆分数组)
我想传递一个字符串数组(以逗号分隔),然后使用一个函数用逗号分隔传递的数组,并添加分隔符来代替逗号。
我将通过一些损坏的代码更详细地向您展示我的意思:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
语法在这里并不重要,您需要了解基类库。另外,你显然想要连接字符串,而不是分割它:
另外,如果你想将 n 个字符串传递给这样的方法,你需要 params 关键字:
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:
Also, if you want to pass n string to a method like that, you need the params keyword:
拆分:
要加入新的分隔符,请将字符串数组传递给
String.Join
:我认为您最好使用 Replace,因为您要做的就是将一个分隔符替换为另一个:
To split:
To join in new delimiter, pass in the array of strings to
String.Join
:I think you are better off using Replace, since all you want to do is replace one delimiter with another:
如果您有多个对象并且想要将它们放入数组中,您可以这样写:
然后您可以简单地将其传递给函数:
C# 有一个很好的技巧,如果您将
params
关键字添加到函数定义,您可以将其视为具有任意数量参数的函数:至于实际实现,最简单的方法是使用
string.Join()
:但是如果您想自己构建结果(例如,如果您想学习如何做到这一点),您可以这样做使用字符串连接 (
oneString + anotherString
),或者更好的是使用StringBuilder
:If you have several objects and you want to put them in an array, you can write:
you can then simply give that to the function:
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:As for the actual implementation, the easiest way is to use
string.Join()
: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, usingStringBuilder
:Split 函数的一个版本采用字符数组。这是一个例子:
One version of the Split function takes an array of characters. Here is an example:
如果您不需要对中间的部分执行任何处理而只需要替换分隔符,则可以使用
String
类上的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 theString
class: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 theString
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.