gpt4 book ai didi

c# - 如何使用 String.IndexOf 方法找到正确的值?

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:43 24 4
gpt4 key购买 nike

我想让我的句子像下面这样,但是我如何使用 String.IndexOf 方法找到 MINVALUE 的正确值,以便在给定条件下从句子中的值更改开始?

句子

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

结果

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

条件

  1. 如果 MINVALUE 具有正值 (8) 则从与以下值相同的值开始最小值 (8)...
  2. 如果 MINVALUE 具有负 (-8) 值,则从与 MAXVALUE (999) 相同的值开始

我的代码

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)

最佳答案

我认为这对你有用;

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);
}

关于c# - 如何使用 String.IndexOf 方法找到正确的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18690897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com