在c#中拆分特殊字符串
Split special string in c#
我想用给定的输出拆分下面的字符串。
谁能帮我做这个。
例子:
- /TEST/TEST123
输出:/测试/
- /TEST1/Test/Test/Test/
输出:/Test1/
- /Text/12121/1212/
输出:/Text/
- /121212121/asdfasdf/
输出:/121212121/
- 12345
输出:12345
我试过 string.split 功能,但效果不佳。我可以实施任何想法或逻辑来实现这种情况。
如果用正则表达式回答对我来说没问题。
您只需要 /
的 Spiting 的第一个结果
string output = input.Split('/')[0];
但是如果你有 //TEST/
并且输出应该是 /TEST
你可以使用正则表达式。
string output = Regex.Matches(input, @"\/?(.+?)\/")[0].Groups[1].Value;
对于您的第 5 种情况:您必须分离逻辑。例如:
public static string Method(string input)
{
var split = input.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if (split.Length == 0) return input;
return split[0];
}
或使用正则表达式。
public static string Method(string input)
{
var matches = Regex.Matches(input, @"\/?(.+?)\/");
if (matches.Count == 0) return input;
return matches[0].Groups[1].Value;
}
一些使用方法的结果:
TEST/54/ => TEST
TEST => TEST
/TEST/ => TEST
public string functionName(string input)
{
if(input.Contains('/'))
{
string SplitStr = input.Split('/')[1];
return "/"+SplitStr .Substring(0, 1) +SplitStr.Substring(1).ToLower()+"/"
}
return input;
}
class Program
{
static void Main(string[] args)
{
string example = "/TEST/TEST123";
var result = GetFirstItem(example);
Console.WriteLine("First in the list : {0}", result);
}
static string GetFirstItem(string value)
{
var collection = value?.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var result = collection[0];
return result;
}
}
StringSplitOptions.RemoveEmptyEntries
是一个枚举,它告诉 Split
函数,当它已将字符串拆分为数组时,如果数组中有元素是空字符串,则该函数不应包含结果中的空元素。基本上你希望集合只包含值。
private void button1_Click(object sender, EventArgs e)
{
string test = @"/Text/12121/1212/";
int first = test.IndexOf("/");
int last = test.Substring(first+1).IndexOf("/");
string finall = test.Substring(first, last+2);
}
我用你所有的例子尝试这段代码并得到正确的输出。试试这个。
下面的方法或许能帮到你。
public string getValue(string st)
{
if (st.IndexOf('/') == -1)
return st;
return "/" + st.Split('/')[1] + "/";
}
我认为这可行:
string s1 = "/TEST/TEST123";
string s2 = "/TEST1/Test/Test/Test/";
string s3 = "/Text/12121/1212/";
string s4 = "/121212121/asdfasdf/";
string s5 = "12345";
string pattern = @"\/?[a-zA-Z0-9]+\/?";
Console.WriteLine(Regex.Matches(s1, pattern)[0]);
Console.WriteLine(Regex.Matches(s2, pattern)[0]);
Console.WriteLine(Regex.Matches(s3, pattern)[0]);
Console.WriteLine(Regex.Matches(s4, pattern)[0]);
Console.WriteLine(Regex.Matches(s5, pattern)[0]);
output = (output.Contains("/"))? '/' +input.Split('/')[1]+'/':input;
我想用给定的输出拆分下面的字符串。 谁能帮我做这个。
例子:
- /TEST/TEST123
输出:/测试/
- /TEST1/Test/Test/Test/
输出:/Test1/
- /Text/12121/1212/
输出:/Text/
- /121212121/asdfasdf/
输出:/121212121/
- 12345
输出:12345
我试过 string.split 功能,但效果不佳。我可以实施任何想法或逻辑来实现这种情况。 如果用正则表达式回答对我来说没问题。
您只需要 /
string output = input.Split('/')[0];
但是如果你有 //TEST/
并且输出应该是 /TEST
你可以使用正则表达式。
string output = Regex.Matches(input, @"\/?(.+?)\/")[0].Groups[1].Value;
对于您的第 5 种情况:您必须分离逻辑。例如:
public static string Method(string input)
{
var split = input.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if (split.Length == 0) return input;
return split[0];
}
或使用正则表达式。
public static string Method(string input)
{
var matches = Regex.Matches(input, @"\/?(.+?)\/");
if (matches.Count == 0) return input;
return matches[0].Groups[1].Value;
}
一些使用方法的结果:
TEST/54/ => TEST
TEST => TEST
/TEST/ => TEST
public string functionName(string input)
{
if(input.Contains('/'))
{
string SplitStr = input.Split('/')[1];
return "/"+SplitStr .Substring(0, 1) +SplitStr.Substring(1).ToLower()+"/"
}
return input;
}
class Program
{
static void Main(string[] args)
{
string example = "/TEST/TEST123";
var result = GetFirstItem(example);
Console.WriteLine("First in the list : {0}", result);
}
static string GetFirstItem(string value)
{
var collection = value?.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var result = collection[0];
return result;
}
}
StringSplitOptions.RemoveEmptyEntries
是一个枚举,它告诉 Split
函数,当它已将字符串拆分为数组时,如果数组中有元素是空字符串,则该函数不应包含结果中的空元素。基本上你希望集合只包含值。
private void button1_Click(object sender, EventArgs e)
{
string test = @"/Text/12121/1212/";
int first = test.IndexOf("/");
int last = test.Substring(first+1).IndexOf("/");
string finall = test.Substring(first, last+2);
}
我用你所有的例子尝试这段代码并得到正确的输出。试试这个。
下面的方法或许能帮到你。
public string getValue(string st)
{
if (st.IndexOf('/') == -1)
return st;
return "/" + st.Split('/')[1] + "/";
}
我认为这可行:
string s1 = "/TEST/TEST123";
string s2 = "/TEST1/Test/Test/Test/";
string s3 = "/Text/12121/1212/";
string s4 = "/121212121/asdfasdf/";
string s5 = "12345";
string pattern = @"\/?[a-zA-Z0-9]+\/?";
Console.WriteLine(Regex.Matches(s1, pattern)[0]);
Console.WriteLine(Regex.Matches(s2, pattern)[0]);
Console.WriteLine(Regex.Matches(s3, pattern)[0]);
Console.WriteLine(Regex.Matches(s4, pattern)[0]);
Console.WriteLine(Regex.Matches(s5, pattern)[0]);
output = (output.Contains("/"))? '/' +input.Split('/')[1]+'/':input;