简体   繁体   中英

Change special chars in string

I am using c# and in code from appsettings.json I take strings and convert them if special chars exists. this is my code

int? a = applicationRequestViewModel.GetApplicantIndex();
int? g = applicationRequestViewModel.GetGurantorIndex();

foreach (var keys in _options.Value.RegisterParamKeys)
{
    string value = keys.Split(";")[0];
    string name = keys.Split(";")[1];
    string key = value.Split(":")[typeOfApplicant];
    key = Regex.Replace(key, @"[^\[a\]]", "[" + a + "]");
    key = Regex.Replace(key, @"[^\[g\]]", "[" + g + "]");
    var registrationProperty = new RegistrationProperty() { };
    registrationProperty.Name = name;
    registrationProperty.Value = (string)rss.SelectToken(key);
    listOfRegistrationProperty.Add(registrationProperty);
}

from appsettings.json I took below strings

"RegisterBatchParams": [
    "applicationInfo.applicationNumber:applicationInfo.applicationNumber:applicationInfo.applicationNumber:applicationInfo.applicationNumber;applicationNumber",
    "applicationInfo.applicantType:applicationInfo.applicantType:applicationInfo.applicantType:applicationInfo.applicantType;applicantType",
    "applicationInfo.customerSegment:applicationInfo.customerSegment:applicationInfo.customerSegment:applicationInfo.customerSegment;customerSegment",
    "applicationInfo.applicationStatusLocalText:applicationInfo.applicationStatusLocalText:applicationInfo.applicationStatusLocalText:applicationInfo.applicationStatusLocalText;applicationStatus",
    "applicationRequestViewModel.applicants[a].businessPartner.person.firstName:applicationRequestViewModel.applicants[a].businessPartner.person.firstName:applicationRequestViewModel.applicants[a].businessPartner.person.firstName:applicationRequestViewModel.applicants[a].businessPartner.person.firstName;customerName"
  ],

for the last string I want to change "applicants[a]" to with index number but it doesn't convert as expected how can I convert correctly?

As expected result

applicationRequestViewModel.applicants[0].businessPartner.person.firstName

but given result

a[0][0][0][0][0]a[0][0][0][0][0][0][0][0][0]a[0][0][0][0][0]a[0][0][0][0][0][0][0][0][0][0]

Instead of @"[^\[a\]]" use @"\[a\]" .

But you don't even need regex for this. Simple string.Replace will do the job just as well.

Or, you can try this regex and replace only char inside of parentheses.

[a](?=[]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM