我想在字符串中基于_分割字符串数组 [英] I want to split string array on basis of _ in the string

查看:82
本文介绍了我想在字符串中基于_分割字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中获取一些值然后我希望拆分这些值然后

从这些值中找到最高数字



< b>我尝试了什么:



我有功能

1)

I am taking some values from database and then i Want split that values and then
find highest number from that values

What I have tried:

I have function
1)

<pre> public DataSet AutogenerateServiceno()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Proc_autogeneratesvno1", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
           
        }



它给我ds的值为


it gives me values in ds as

sv_10
sv_11
sv_12
sv_4
sv_9





2)我从另一个函数调用上面的函数bellow





2) I am calling above function from another function as bellow

 public string AutogenerateSvno()
        {
            string s2 = "sv_";
            DataSet ds = objdal.AutogenerateServiceno(); //  here values in s=sv_10, sv_11, sv_12, sv_4, sv_9.
// now I want seperate Sv and number seperate and want to insert number in one // //arrar and number values in another array i.e values 10,11,12,4,9 in one array.
// then I want to find Highest Number from numeric array and in that number //increment by one and then concate that incremented number to sv_(number) and //return that values to presentation layer  for that I have written following code.
//but it giving exception at  
                arrval1[i] =Convert.ToString(arrvalues[i].Split('_'));
           
 string[] arrvalues = new string[ds.Tables[0].Rows.Count];
            string[] arrval1 = new string[ds.Tables[0].Rows.Count];
            int[] arrval = new int[ds.Tables[0].Rows.Count];

            //loopcounter
            for (int loopcounter = 0; loopcounter < ds.Tables[0].Rows.Count; loopcounter++)
            {
                string str;
               // .Split('_');
                //assign dataset values to array
                arrvalues[loopcounter] =ds.Tables[0].Rows[loopcounter]["svno"].ToString();
            }
            for (int i = 0; i < arrval.Length - 1; i++)
            {
                arrval1[i] =Convert.ToString(arrvalues[i].Split('_'));  // Error Here
                arrval[i] = int.Parse(arrval1[i]);
            }
            //string[] s1 = s.Split('_');
            //int i = int.Parse(s1[1]);
            //i++;
            //s2 = s2 + i
            return s2;
        }

推荐答案

我们无法分辨 - 太多取决于您的数据(我们无法访问)以及代码运行时的其他因素(我们无法做到,因为我们无法访问您的数据)。

因此,它将取决于您。

在函数的第一行放置一个断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
We can't tell - too much depends on your data (which we don't have access to) and other factors while the code is running (which we can't do, because we don't have access to your data).
So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


我猜这就是你正在寻找:

I'm guessing that this is what you are looking for:
var list = new List<string>()
    { "sv_10", "sv_11", "sv_12", "sv_4", "sv_9" };

var best = list.OrderByDescending(x => int.Parse(x.Split(new[] { '_' }).Last()))
                .First();


这篇关于我想在字符串中基于_分割字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆