gpt4 book ai didi

C#如何提取字符串的一部分

转载 作者:太空宇宙 更新时间:2023-11-03 17:13:35 25 4
gpt4 key购买 nike

如何从字符之间用“-”分隔的字符串中提取一部分?例如,我想从“123-4567-9012”中提取“123”或者从“123-4567-9012”中提取“4567”甚至来自同一个字符串的 9012

最佳答案

只需拆分 - 字符,稍后您就可以访问它们。使用 string.Split(char[])

string str  = "123-4567-9012";
string[] arr = str.Split('-');

它会导致:

arr[0] = "123";
arr[1] = "4567";
arr[2] = "9012"

关于C#如何提取字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13837276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com