Is there any way to get the portion of string after the last "_" forexample;
string1 = "A_110_23_09_ABC"
result1 = "ABC"
string2 = "A_110_23_09_ABC_555"
result2 = "555"
var s = "A_110_23_09_ABC";
var result = s.Substring(s.LastIndexOf('_') + 1);
If you are using .NET 3.5 or higher, you could do it by LINQ to Object. The code would be something like :
var result = string.Split('_').Last();
Related
I have one trouble with custom formatting string.
I want to make smth like that:
var str = "SOME_ORIGINAL_FIELD_NAME";
var format1 = "XX_X_X";
var format2 = "X_XXX";
var strFormat1 = String.Format(str, format1); // SOMEORIGINAL_FIELD_NAME
var strFormat2 = String.Format(str, format2); // SOME_ORIGINALFIELDNAME
Does anybody know the right direction for searching? Maybe I should look at IFormatProvider and ICustomFormatter side.
Sure, you just have to:
split the source string into its components,
use {i} placeholders instead of X, and
reverse the parameters to String.Format (format is first, data follows).
Example code (fiddle):
var components = "SOME_ORIGINAL_FIELD_NAME".Split('_');
var format1 = "{0}{1}_{2}_{3}";
var format2 = "{0}_{1}{2}{3}";
var strFormat1 = String.Format(format1, components); // SOMEORIGINAL_FIELD_NAME
var strFormat2 = String.Format(format2, components); // SOME_ORIGINALFIELDNAME
Replace the X's in the format string with successive placeholders, and split the input string value into a string array, then apply string.Format():
public static string FormatSplitAndJoin(string input, string formatTemplate, string delimiter = "_", string placeholder = "X")
{
// split "a_b_c" into ["a", "b", "c"]
var parts = input.Split(delimiter);
// turn "X_X_X" into "{0}_{1}_{2}"
var index = 0;
var formatString = Regex.Replace(formatTemplate, placeholder, m => string.Format("{{{0}}}", index++));
// validate input length
if(index > parts.Length)
throw new ArgumentException(string.Format("input string resulted in fewer arguments than expected, {0} placeholders found", index));
// apply string.Format()
return string.Format(formatString, parts);
}
Now you can do:
var str = "SOME_ORIGINAL_FIELD_NAME";
var format1 = "XX_X_X";
var format2 = "X_XXX";
var strFormat1 = FormatSplitAndJoin(str, format1); // SOMEORIGINAL_FIELD_NAME
var strFormat2 = FormatSplitAndJoin(str, format2); // SOME_ORIGINALFIELDNAME
What I would like to do is something like this:
var a = "Hello 123";
but instead code it like this:
var id = 123;
var a = xxxx("Hello ?", id);
Is there some function I can replace the xxxx with that will help me to do this in C#
There is.
String.Format("Hello {0}", id);
String format uses numbered indexes to bind data into the string.
And as of C# 6 onwards you can use the following string interpolation:
var a = $"Hello {id}";
If I take that literally, replacing the ?:
string a = "Hello ?".Replace("?", id.ToString());
but normally we do
string a = String.Format("Hello {0}", id);
or
string a = $"Hello {id}";
var id = 123;
var a = $"Hello {id}";
You can either concatenate using +:
var a = "Hello " + id;
string.Format:
var a = string.Format("Hello {0}", id);
or string interpolation:
var a = $"Hello {id}";
I have sql string like
select * from dbo.Person where Person = ? AND Name = ? OR (Country = ? OR City = 1)
If it's possible to get string array like below with Regex in C#
result[0] = Person = ?
result[1] = Name = ?
result[2] = (Country = ? OR City = 1)
thanks.
First try looks like this
var s = #"select* from dbo.Person where Person = ? AND Name = ? OR (Country = ? OR City = 1)";
var reg = new Regex("[A-Za-z]+ = [A-Za-z0-9?]+");
var result = reg.Matches(s);
Something like that but is no Regex
var s = #"select* from dbo.Person where Person = ? AND Name = ? OR(Country = ? OR City = 1)";
var s1 = s.Split(new[] { "where" }, StringSplitOptions.None)[1];
var s2 = s1.Split(new[] { "OR", "AND" }, StringSplitOptions.None);
If you need anything more complicated than this, it's going to quickly go beyond what you can easily solve with regex. I have released a free parser on GitHub that will parse out TSQL in a stable way into the pieces, TSQL Parser . You can also use the Microsoft TSQL parser, using the TSqlParser . With either of these, they will break it out a little more granular than you're requesting, which you will then have to piece back together based on parenthesis for example.
If i have string like this:
string a = "<=1.0.5" or "=1.0.5" or "!=1.0.5";
How can i get the part with operator and the part with version number separately into variables?
Some kind of regex:
var str = "=1.0.5";
var regex = new Regex("([<>=!]+)(.*)");
var result = regex.Match(str);
Console.WriteLine(result.Groups[1].Value);
Console.WriteLine(result.Groups[2].Value);
If equal operator always present in the string:
string a = "<=1.0.5"; // "=1.0.5" or "!=1.0.5" or "==1.0.5"
int indx = a.LastIndexOf('=') + 1;
string op = a.Substring(0, indx); //<=
string version = a.Substring(indx); //1.0.5
string TEST1 = "abc def"
classA.detailList= (from tbl1 in ds.Table[0].AsEnumerable()
where tbl1["column1"] = 'aaaa'
select new classdetail(){
Var A = ToString().Substring(0, tbl1["Vehicle_Make_Model"].ToString ().IndexOf(' ')).Trim()
Var B = ToString().Substring(tbl1["Vehicle_Make_Model"].ToString().IndexOf(' ') + 1).Trim()
}).ToList();
It works fine if i have string as TEST1 but how to handle if
TEST1="abc"
want to check whether second part is exists after space or not?
Please help me here
you can try something like this:
string[] outputParts = TEST1.Trim().Split(' ');
if (outputParts.Length == 2)
{
//do something
}