string 如何将字符串附加到数据集中的其他字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11648968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append strings to other strings in a data set?
提问by t1red
I want to append several strings in a data set with custom strings.
我想在带有自定义字符串的数据集中附加几个字符串。
Example
例子
Content of Dataset:
数据集内容:
Test1
Test2
Test3
Result after appending:
追加后的结果:
Test1.com
Test2.com
Test3.com
Would I have to use regex to parse to the end of each Test[n] to be able to append it with a custom string (.com
)? Has anyone got an example that describes exactly how to do it?
我是否必须使用正则表达式解析到每个 Test[n] 的末尾才能附加自定义字符串 ( .com
)?有没有人有一个例子来准确描述如何做到这一点?
I am reading from a SQL-Table and writing values into a DataSet which is exported to CSV the following way:
我正在从 SQL 表中读取并将值写入数据集,该数据集通过以下方式导出到 CSV:
$DataSet.Tables[0] | ConvertTO-Csv -Delimiter ',' -NotypeInformation |`% { $_ -replace '"','' } | out-file $outfile -Encoding "unicode"
The DataSet contains of Strings such as:
DataSet 包含字符串,例如:
Banana01
Banana02
Apple01
Cherry01
Cherry02
Cherry03
The thing I want to do is append .com
to only Cherry01
, Cherry02
, and Cherry03
, and after appending .com
, export it as a CSV file.
我想要做的事情是追加.com
只Cherry01
,Cherry02
以及Cherry03
和追加后.com
,将其导出为CSV文件。
回答by Shay Levy
There are many ways. Here are a few:
有很多方法。以下是一些:
# Using string concatenation
'Test1','Test2','Test3' | Foreach-Object{ $_ + '.com' }
# Using string expansion
'Test1','Test2','Test3' | Foreach-Object{ "$_.com" }
# Using string format
'Test1','Test2','Test3' | Foreach-Object{ "{0}{1}" -f $_,'.com' }
回答by justinf
You could use something like this:
你可以使用这样的东西:
Example 1
示例 1
$t = "test"
$t = $t + ".com"
Example 2
示例 2
$test = @("test1","test2")
$test | ForEach-Object {
$t = $_ + ".com"
Write-Host $t}
With your added code I did this. I don't have a database to test it on, so I made the data set manually, so you might just have to change the $DataSet[0]in my code to $DataSet.Tables[0].
使用您添加的代码,我做到了。我没有用于测试的数据库,因此我手动创建了数据集,因此您可能只需将代码中的$DataSet[0]更改为$DataSet.Tables[0]。
$DataSet[0] | ConvertTO-Csv -Delimiter ',' -NotypeInformation | Foreach-Object{$T=$_
IF($T -match "(Cherry\d\d)"){$T = $T -replace "(Cherry\d\d)(.+)",'.com'};$T } | out-file $outfile -Encoding "unicode"
回答by SpellingD
$array | %{if($_ -match "^Cherry\d\d"){$_ += ".com"};$_}
$array | %{if($_ -match "^Cherry\d\d"){$_ += ".com"};$_}