0

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万元

7
  • 1
    use string interpolation : learn.microsoft.com/en-us/dotnet/csharp/language-reference/… Commented May 3, 2018 at 6:04
  • 1
    @sLw You would do it in the same way you do it with string.Format - you process the data before interpolating Commented May 3, 2018 at 6:09
  • 1
    @Bassie but why would you first use string.remove and then interpolate? that's hardcoding at its best Commented May 3, 2018 at 6:10
  • 1
    Maybe you could actually explain the rules of what you're trying to achieve, rather than just providing a single example in a language that will likely be unfamiliar to most. There are a hundred ways and more to get that result from that data but, unless we know what the actual rules are, we have no idea whether they would work for other data. In short, provide a FULL and CLEAR explanation of the problem. Commented May 3, 2018 at 6:14
  • 1
    @Foskill just use Regex.Replace() and you are good to go. You can specify that it only replaces the first occurrence. Commented May 3, 2018 at 6:20

2 Answers 2

1

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万元"
Sign up to request clarification or add additional context in comments.

Comments

1
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,'万元'});

4 Comments

and how are you replaceing the %s? this is way too hardcoded
Thank you for your answer, but have you implemented it in C#?
Thank you for your answer, but my data [Reason] is var.This will not be realized.
I think in c# you need to change %s and %d to parameters {0} , {1} , {2} and try that way :D. Hope that helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.