简体   繁体   中英

how can I use String.IndexOf method to find the correct value?

I want to make my sentence like below, but how can I use String.IndexOf method to find the correct value of MINVALUE to make start with value change in sentence with given conditions?

sentence

CREATE SEQUENCE "MY_TEST_SEQUENCE" MINVALUE 8 MAXVALUE 999 INCREMENT BY 1 START WITH 55 CACHE 20 NOORDER NOCYCLE

result

CREATE SEQUENCE "MY_TEST_SEQUENCE" MINVALUE 8 MAXVALUE 999 INCREMENT BY 1 START WITH 8 CACHE 20 NOORDER NOCYCLE

Conditions

  1. if MINVALUE has plus value (8) than make start with value same as MINVALUE (8)...
  2. if MINVALUE has minus (-8) value than make start with value same as MAXVALUE (999)

My Code

var startvalue = 0;
if(MINVALUE > 0)
{
    startvalue = MINVALUE;
}
Else if(MINVALUE < 0)
{
    startvalue = MAXVALUE;
}
var result = String.Format(@"CREATE SEQUENCE ""MY_TEST_SEQUENCE"" MINVALUE 8 MAXVALUE 999 INCREMENT BY 1 START WITH {0}  CACHE 20 NOORDER NOCYCLE",startvalue)

Seems like a regular expression with capture groups would be a better fit than trying to break the string into pieces.

var regex = new Regex(@".*\s+MINVALUE\s+(-?\d*)\s+MAXVALUE\s+(-?\d*)\s+.*");
var sentence = @"CREATE SEQUENCE ""MY_TEST_SEQUENCE"" MINVALUE 8 MAXVALUE 999 INCREMENT BY 1 START WITH 55 CACHE 20 NOORDER NOCYCLE";

var match = regex.Match(sentence);

if (match.Groups.Count != 3)
{
    throw new ApplicationException("input string not in expected format");
}

var minValue = int.Parse(match.Groups[1].Value);
var maxValue = int.Parse(match.Groups[2].Value);

var result = string.Format(@"CREATE SEQUENCE ""MY_TEST_SEQUENCE"" MINVALUE 8 MAXVALUE 999 INCREMENT BY 1 START WITH {0}  CACHE 20 NOORDER NOCYCLE", minValue < 0 ? maxValue : minValue);

I think this should work for you;

string sentence = @"CREATE SEQUENCE 'MY_TEST_SEQUENCE' MINVALUE -8 MAXVALUE 999 INCREMENT BY 1 START WITH 55 CACHE 20 NOORDER NOCYCLE";
string pattern = @".*\s+MINVALUE[ ]*(?<MinValue>[-?\d]*)[ ]*MAXVALUE[ ]*(?<MaxValue>[\d]*)";
Regex rgx = new Regex(pattern);
Match match = rgx.Match(sentence);
Group gp1 = match.Groups[1];//-8 or 8 (both valid).
Group gp2 = match.Groups[2];//Value after MAXVALUE.
if (gp1.Value.StartsWith("-") == false)//No minus sign,that must be a positive value.
{
  string s2 = string.Format(@"CREATE SEQUENCE ""MY_TEST_SEQUENCE"" MINVALUE 8 MAXVALUE 999  INCREMENT BY 1 START WITH {0} CACHE 20 NOORDER NOCYCLE", gp1.Value);
  MessageBox.Show(s2);
}

else if (gp1.Value.StartsWith("-") == true)//No need for further checking, a value with - will always be smaller.
{
  string s2 = string.Format(@"CREATE SEQUENCE ""MY_TEST_SEQUENCE"" MINVALUE 8 MAXVALUE 999 INCREMENT BY 1 START WITH {0} CACHE 20 NOORDER NOCYCLE", gp2.Value);
  MessageBox.Show(s2);
}

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