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
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
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
In my SQL Database, have a data:
Reason:%s与%s等预算科目的内容存在重复,核减%d万元
ResultS1:本科目镍铁中多元素样品前处理
ResultS2:本科目ICP测定法
ResultD:2.50
I want get a string:本科目镍铁中多元素样品前处理与本科目ICP测定法等预算科目的内容存在重复,核减2.50万元
You can use the helpful extension method from this question (second answer down) ReplaceFirst:
public static string ReplaceFirst(this string text, string search, string replace)
{
int pos = text.IndexOf(search);
if (pos < 0)
{
return text;
}
return $"{text.Substring(0, pos)}{replace}{text.Substring(pos + search.Length)}";
}
and then call it like this
var reason = "%s与%s等预算科目的内容存在重复,核减%d万元";
var results1 = "本科目镍铁中多元素样品前处理";
var results2 = "本科目ICP测定法";
var resultd = "2.50";
var final = reason
.ReplaceFirst("%s", results1)
.ReplaceFirst("%s", results2)
.ReplaceFirst("%d", resultd);
Where final is assigned
"本科目镍铁中多元素样品前处理与本科目ICP测定法等预算科目的内容存在重复,核减2.50万元"
SELECT concat_ws('',ResultS1,'与',ResultS2,'等预算科目的内容存在重复,核减',ResultD,'万元') as Result
FROM `yourtable`
Follow the answer concatenate string in c#
string key = String.Join("", new String[] { ResultS1,'与',ResultS2,'等预算科目的内容存在重复,核减',ResultD,'万元'});
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();
if (!ruleSet.Validate(rv))
{
List<System.Workflow.Activities.Rules.Rule> rulesList = new List<System.Workflow.Activities.Rules.Rule>();
rulesList = ruleSet.Rules.ToList();
foreach (System.Workflow.Activities.Rules.Rule rr in rulesList)
{
System.Workflow.Activities.Rules.Rule rules = new System.Workflow.Activities.Rules.Rule();
string ruleName = rr.Name;
string condtion = rr.Condition.ToString();
string s1 = rr.ThenActions.ToString();
string[] s2 = s1.Split('=');
string rightString = s2[1];
string append = "=Convert.ToString(" + rightString + ")";
string result = s2[0] + append;
rules.ThenActions=result;//Error:cannot convert string to ruleaction
}
}
How to convert type string to System.Workflow.Activities.Rules.RuleAction in c#..
i want to modify the ThenActions,ElseActions,Condition in code behind without opening the RulesEditor.
If you want to change your actions, don't convert it to a string, but change the RuleAction object.
Depending on the type of the action you should cast it and change it:
var ra = (RuleStatementAction)rr.ThenActions[0];
ra.XXX = yourChange;
rules.ThenActions[0] = ra;
RETURN (REPLACE(REPLACE(REPLACE(#input, #class_text, '~~|~'), #id_text, #class_text), '~~|~', #id_text))
where #input is string.
How to convert this in C# with the same parameters...
#class_text is class_text in C#, etc..
input = input.Replace(class_text, "~~|~").Replace(id_text, class_text).Replace("~~|~", id_text);
simple as that
T-SQL's REPLACE actually has different arguments. E.g.,
REPLACE(string1, string2, replaceText)
Where it is searching for string1 within string2, whereas, it is generally:
string1.Replace(string2, replaceText)
...in .NET, where you are searching for string2 in string1. For large amounts of string replaces, I'd recommend the StringBuilder.
var builder = new Stringbuilder(input);
builder = builder.Replace(class_text, "~~|~").Replace(id_text, class_text).Replace("~~|~", id_text)
string result = builder.ToString();
1) make a string
String myOldString = "No way";
2) Use the replace function
String myNewString = myOldString.Replace("No", "Yes");
Here's the MSDN for more detailed information: http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx#Y480
EDIT
Here's the data from your post:
String myInput = "whatever #input equals"
String myclass_text = "whatever #class_text equals"
String myid_text = "whatever #id_text equals"
String myString = myInput.replace(myclass_text, "~~|~")
myString = myString.replace(myid_text, class_text)
myString = myString.replace("~~|~", myid_text)
Or all-in-one (I don't know if c# lets you piggy-back like this, but if prefer one-liners, give it a try:
String myString = myInput.replace(myclass_text, "~~|~").replace(myid_text, myclass_text).replace("~~|~", myid_text)
Converted the TSQL to C#, which could have been requested in original question How to change string element position in C#
/// <summary>
/// Given a line of text, swap position of class and id tags.
/// Id should always precede class tag
///
/// </summary>
/// <param name="input"><td class="m92_t_col5" id="preis_0">xx</td></param>
/// <returns><td id="preis_0" class="m92_t_col5">xx</td></returns>
public static string SwapClassAndId(string input)
{
string output = string.Empty;
int classOrdinal = 0;
int classOridnalEndQuotes = 0;
string classText = string.Empty;
int idOrdinal = 0;
int idOrdinalEndQuotes = 0;
string idText = string.Empty;
classOrdinal = input.IndexOf("class=");
classOridnalEndQuotes = input.IndexOf("\"", classOrdinal + 7) + 1;
idOrdinal = input.IndexOf("id=");
idOrdinalEndQuotes = input.IndexOf("\"", idOrdinal + 4) + 1;
if (idOrdinal < classOrdinal)
{
return input;
}
classText = input.Substring(classOrdinal, classOridnalEndQuotes - classOrdinal);
idText = input.Substring(idOrdinal, idOrdinalEndQuotes - idOrdinal);
output = input.Replace(classText, "~~|~").Replace(idText, classText).Replace("~~|~", idText);
return output;
}