Reputation: 1911
I need to write an function in c# that trims down values from an string and should check in the database whether it is available or not .
Consider the string be "test_test1_test2_test3"
i have to split the values above string based on underscore. i.e initial i have to take "test3
" from the above string and check in db whether it is available.
If not available then i have to take test2_test3
and check in db . If not available then i have to take test1_test2_test3
and check in db till its value is available .
Can someone help me with this ??
Sample Code :
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
}
Upvotes: 2
Views: 138
Reputation: 2967
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);
}
Upvotes: 0
Reputation: 3744
You can get a list
of all possible variations by next:
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));
}
And i suggest to check all of them in one query.
Upvotes: 0
Reputation: 1205
This would be my answer this way it is nicely divided into several functions
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;
}
Upvotes: 0
Reputation: 6050
You can use in-build Split method of string class. This method is split the string based upon a specific character (in your case it is underscore "_") and make a string array. Further you can iterate this string array and implement your logic there.
For E.g.
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;
}
}
Upvotes: 0
Reputation: 14624
You can Split
the string and loop through array
and append values in string which you want to match to database.
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];
}
Upvotes: 2