C#-拆分字符串的问题
我有一组值,根据这些值我分割了字符串C#-拆分字符串的问题,c#,string,split,C#,String,Split,我有一组值,根据这些值我分割了字符串 string[] seperator = new string[9]; seperator[0] = "*"; //is the client seperator[1] = "/"; //is the name of company seperator[2] = "("; //name of the market seperator[5] = ":"; //ID
string[] seperator = new string[9];
seperator[0] = "*"; //is the client
seperator[1] = "/"; //is the name of company
seperator[2] = "("; //name of the market
seperator[5] = ":"; //ID
seperator[6] = "?"; //orderType
seperator[3] = "!@"; //realtive Time
seperator[4] = "!+"; //
seperator[7] = "+"; //quantity
seperator[8] = "@";//price
string[] result = values.Split(seperator, StringSplitOptions.None);
例如:输入字符串是
*A/AB(M!@12:6?简单!+5+2
OUTPUT
[0]: ""
[1]: "A"
[2]: "AB"
[3]: "M"
[4]: "12"
[5]: "6"
[6]: "SIMPLE"
[7]: "5"
[8]: "2"
输出
[0]: ""
[1] :“A”
[2] :“AB”
[3] :“M”
[4]: "12"
[5]: "6"
[6] :“简单”
[7]: "5"
[8]: "2"
例如:输入字符串是
*A(M!@12?简单!+5+2/AB:6
OUTPUT:
[0]: ""
[1]: "A"
[2]: "M"
[3]: "12"
[4]: "SIMPLE"
[5]: "5"
[6]: "2"
[7]: "AB"
[8]: "6"
输出:
[0]: ""
[1] :“A”
[2] :“M”
[3]: "12"
[4] :“简单”
[5]: "5"
[6]: "2"
[7] :“AB”
[8]: "6"
我面临的问题是:我怎样才能说明A是客户,AB是公司等等
作为用户可以随机输入此信息的顺序。。。
如果他没有输入这些值中的任何一个,则会更改结果长度?使用一个或多个正则表达式命名捕获组并按名称索引匹配项如何 例如,检查或 下面是一个让您开始学习的示例:
using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
Regex regex = new Regex(@"(?:\*(?<client>\w+))|(?:/(?<company>\w+))",RegexOptions.Compiled);
string input = "*A/AB(M!@12:6?SIMPLE!+5+2";
foreach (Match match in regex.Matches (input)) {
if (match.Groups["client"].Success) {
Console.WriteLine("Client = {0}", match.Groups["client"].Value);
} else if (match.Groups["company"].Success) {
Console.WriteLine("Company = {0}", match.Groups["company"].Value);
}
}
}
}
使用系统;
使用System.Text.RegularExpressions;
班级计划{
静态void Main(字符串[]参数){
Regex Regex=new Regex(@“(?:\*(?\w+))|(?:/(?\w+)”,RegexOptions.Compiled);
字符串输入=“*A/AB(M!@12:6?简单!+5+2”;
foreach(regex.Matches中的匹配(输入)){
if(匹配组[“客户端”]。成功){
WriteLine(“Client={0}”,match.Groups[“Client”].Value);
}else if(匹配组[“公司”]。成功){
WriteLine(“Company={0}”,match.Groups[“Company”].Value);
}
}
}
}
我知道正则表达式的语法一开始似乎很难理解,但无论何时需要执行此类文本操作,它们都是非常强大的工具
此外,还有一些工具可以让您进行实验并帮助您编写正则表达式,如和。如何使用一个或多个带有命名捕获组的正则表达式并按名称索引匹配项 例如,检查或 下面是一个让您开始学习的示例:
using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
Regex regex = new Regex(@"(?:\*(?<client>\w+))|(?:/(?<company>\w+))",RegexOptions.Compiled);
string input = "*A/AB(M!@12:6?SIMPLE!+5+2";
foreach (Match match in regex.Matches (input)) {
if (match.Groups["client"].Success) {
Console.WriteLine("Client = {0}", match.Groups["client"].Value);
} else if (match.Groups["company"].Success) {
Console.WriteLine("Company = {0}", match.Groups["company"].Value);
}
}
}
}
使用系统;
使用System.Text.RegularExpressions;
班级计划{
静态void Main(字符串[]参数){
Regex Regex=new Regex(@“(?:\*(?\w+))|(?:/(?\w+)”,RegexOptions.Compiled);
字符串输入=“*A/AB(M!@12:6?简单!+5+2”;
foreach(regex.Matches中的匹配(输入)){
if(匹配组[“客户端”]。成功){
WriteLine(“Client={0}”,match.Groups[“Client”].Value);
}else if(匹配组[“公司”]。成功){
WriteLine(“Company={0}”,match.Groups[“Company”].Value);
}
}
}
}
我知道正则表达式的语法一开始似乎很难理解,但无论何时需要执行此类文本操作,它们都是非常强大的工具
此外,还有一些工具可以让您进行实验,并帮助您编写正则表达式,如和。使用类似的工具
SortedList<int, string> list = new SortedList<int, string>();
string[] seperator = new string[9];
seperator[0] = "*"; //is the client
seperator[1] = "/"; //is the name of company
seperator[2] = "("; //name of the market
seperator[5] = ":"; //ID
seperator[6] = "?"; //orderType
seperator[3] = "!@"; //realtive Time
seperator[4] = "!+"; //
seperator[7] = "+"; //quantity
seperator[8] = "@";//price
string val = "*A/AB(M!@12:6?SIMPLE!+5+2";
for (int iSep = 0; iSep < seperator.Length; iSep++)
list.Add(val.IndexOf(seperator[iSep]), val);
SortedList=new SortedList();
字符串[]分隔符=新字符串[9];
分隔符[0]=“*”;//是客户端
分隔符[1]=“/”;//是公司名称
分隔符[2]=“(”;//市场名称
分隔符[5]=“:”;//ID
分隔符[6]=“?”;//订单类型
分隔符[3]=“!@”//实时
分隔符[4]=“!+”//
分隔符[7]=“+”;//数量
分隔符[8]=“@”//价格
string val=“*A/AB(M!@12:6?简单!+5+2”;
for(int-iSep=0;iSep
将为您提供分隔符开始的位置列表,用户输入的顺序可以任意,然后您可以使用子字符串来检索值
SortedList<int, string> list = new SortedList<int, string>();
string[] seperator = new string[9];
seperator[0] = "*"; //is the client
seperator[1] = "/"; //is the name of company
seperator[2] = "("; //name of the market
seperator[5] = ":"; //ID
seperator[6] = "?"; //orderType
seperator[3] = "!@"; //realtive Time
seperator[4] = "!+"; //
seperator[7] = "+"; //quantity
seperator[8] = "@";//price
string val = "*A/AB(M!@12:6?SIMPLE!+5+2";
for (int iSep = 0; iSep < seperator.Length; iSep++)
list.Add(val.IndexOf(seperator[iSep]), val);
SortedList=new SortedList();
字符串[]分隔符=新字符串[9];
分隔符[0]=“*”;//是客户端
分隔符[1]=“/”;//是公司名称
分隔符[2]=“(”;//市场名称
分隔符[5]=“:”;//ID
分隔符[6]=“?”;//订单类型
分隔符[3]=“!@”//实时
分隔符[4]=“!+”//
分隔符[7]=“+”;//数量
分隔符[8]=“@”//价格
string val=“*A/AB(M!@12:6?简单!+5+2”;
for(int-iSep=0;iSep
将为您提供分隔符开始的位置列表,用户输入的顺序可以任意,然后您可以使用子字符串检索值如何对输入字符串执行一些替换以将其转换为更易于管理的格式。例如
inputString = inputString.Replace("*", ",Client=").Replace("/", ",Company=");
然后,您可以在“,”上拆分字符串列表及其标题,然后在“=”上拆分这些字符串以获得标题和值。对输入字符串执行多次替换以将其转换为更易于管理的格式如何。例如
inputString = inputString.Replace("*", ",Client=").Replace("/", ",Company=");
然后,您可以在“,”上拆分,并获取带有标题的字符串列表,然后在“=”上拆分这些字符串获取标题和值。如果没有标识字段的标记,并且字段顺序是随机的,我看不出您将如何执行此操作抱歉,我只看到没有标识如果没有标识字段的标记,并且字段顺序是随机的,我看不到您将如何执行此操作抱歉,我只看到没有标识火种