C# 修剪字符串中的值并检入数据库
我需要在c#中编写一个函数,用于从字符串中裁剪值,并应在数据库中检查该值是否可用 假设字符串是“test\u test1\u test2\u test3” 我必须根据下划线拆分字符串上方的值。i、 e initial我必须从上面的字符串中提取“C# 修剪字符串中的值并检入数据库,c#,C#,我需要在c#中编写一个函数,用于从字符串中裁剪值,并应在数据库中检查该值是否可用 假设字符串是“test\u test1\u test2\u test3” 我必须根据下划线拆分字符串上方的值。i、 e initial我必须从上面的字符串中提取“test3”,并在db中检查它是否可用。 如果不可用,则我必须进行test2\u test3并签入数据库。如果不可用,则我必须使用test1\u test2\u test3并签入db,直到其值可用 有人能帮我吗 示例代码: string name = "t
test3
”,并在db中检查它是否可用。
如果不可用,则我必须进行test2\u test3
并签入数据库。如果不可用,则我必须使用test1\u test2\u test3
并签入db,直到其值可用
有人能帮我吗
示例代码:
string name = "test_test1_test2_test3";
int strIndex = name.LastIndexOf('_');
string value = name >= 0 ? name.Substring(strIndex + 1) : "" ;
if(checkValue(value)){
}else{
// Now i have to repeat the above process and take the next part of string and check for its availablity in db .
// i.e for test2_test3
}
您可以
Split
字符串,并在array
中循环,并在字符串中附加要与数据库匹配的值
string name = "test_test1_test2_test3";
string[] arr = name.Split('_');
string strToMatch = string.Empty;
for (int i = arr.Length -1; i >= 0; i--)
{
if (i != arr.Length - 1)
strToMatch = arr[i] + "_" + strToMatch;
else
strToMatch = arr[i];
}
您可以使用string类的内置方法。此方法基于特定字符分割字符串(在您的示例中为下划线“\”),并生成字符串数组。此外,您可以迭代这个字符串数组并在那里实现您的逻辑
例如
string name = "test_test1_test2_test3";
var stringArray = name.Split('_');
foreach (var item in stringArray)
{
//Call to DB, for checking string exists or not
If (StringExists)
{
break;
}
}
这就是我的答案,它被很好地划分为几个函数
private void functionX()
{
string name = "test_test1_test2_test3";
string[] splitValues = name.Split('_');
bool notIndDB = false;
int startIndex = splitValues.Length;
while (!notIndDB && startIndex >= 0)
{
notIndDB = Check(BuildString(startIndex--, splitValues));
}
}
private string BuildString(int startIndex, string[] splitValues)
{
StringBuilder builder = new StringBuilder();
for (int i = startIndex; i <= splitValues.Length -1; i++)
{
builder.Append((i != splitValues.Length -1 ? splitValues[i] + "_" : splitValues[i]));
}
return builder.ToString();
}
private bool Check(string parameter)
{
bool isFound = false;
//Check if string is in db
return isFound;
}
private void functionX()
{
string name=“test\u test1\u test2\u test3”;
string[]splitValues=name.Split('');
bool notIndDB=false;
int startIndex=splitValues.Length;
而(!notIndDB&&startIndex>=0)
{
notIndDB=Check(BuildString(startIndex--,splitValues));
}
}
私有字符串BuildString(int startIndex,string[]splitValues)
{
StringBuilder=新的StringBuilder();
对于(int i=startIndex;i您可以通过下一步获得所有可能变化的列表
:
var str = "test_test1_test2_test3";
var arr = str.Split('_').ToList();
var list = new List<string> { str };
while (arr.Count > 1)
{
arr.RemoveAt(0);
list.Add(string.Join("_", arr));
}
var str=“test\u test1\u test2\u test3”;
var arr=str.Split(“”“).ToList();
var list=新列表{str};
而(arr.Count>1)
{
arr.RemoveAt(0);
list.Add(string.Join(“_”,arr));
}
我建议在一个查询中检查所有这些。< /P>工作。如果在DB中可用的STRAMTACH,我如何能够前后剪切文本并将其存储为单独的字符串。可以从原始字符串中获取子字符串并存储在另一个变量中。
string name = "test_test1_test2_test3";
int pos = name.LastIndexOf("_");
while (pos > -1)
{
string test_string = name.Substring(pos + 1);
Console.WriteLine(test_string);
pos = name.LastIndexOf("_", pos - 1);
}