Usage of IndexOf in C#

Recently, I encountered a time format conversion problem during the reconfiguration of the computer room for the time and price of the computer. How to convert the time that has been used on the computer? More concise, clear and beautiful?

My logic is like this. After the customer logs in on the computer, the login time is obtained. The time used by the customer is equal to the current time minus the login time of the customer on the computer.

(Elapsed time = current time-computer login time) The available time is very unsightly.

Through the online query, the combination of the IndexOf function and the substring() function is used to obtain the conversion of the time format. The converted result is shown below

Is this much more beautiful.

The following is the code display:

public void TimeBilling()
        {
            Facade.TimeBillingFacade timeBillingFacade = new Facade.TimeBillingFacade();
            List<Entity.CustomerActivation> list1 = customerActivationFacade.GetCustomerActivationList1();//返回当前已上机用户的卡号
            for (int i = 0; i < list1.Count; i++)
            {
                string identityNo = list1[i].IdentityNo;

                List<Entity.CustomerActivation> list = customerActivationFacade.GetCustomerActivationByIdentityNo(identityNo);//返回当前已上机用户的开始上机时间
                for (int j = 0; j < list.Count; j++)
                {
                    string BeginTime = list[j].BeginTime;
                    string Time = Convert.ToString(DateTime.Now - Convert.ToDateTime(BeginTime));//此时间为用户的上机已用时间

                    //转换时间格式
                    int a = Time.IndexOf(':');//使用IndexOf()函数从前往后找出时间冒号:在时间Time中的索引位置,并将返回结果赋值给int型变量a
                    int price = Convert.ToInt32(Time.Substring(a + 1, 2));//Time.Substring(a + 1, 2)为截取从a+1到2位以内的字符串
                    int day = Convert.ToInt32(Time.Substring(0, 1)) * 24;
                    int hour = Convert.ToInt32(Time.Substring(a - 2, 2));
                    string Hour = Convert.ToString(day + hour);
                    string time = Hour + "时" + Time.Substring(a + 1, 2) + "分";//Time.Substring(a + 1, 2)为截取从a+1到2位以内的字符串

                    //timeBillingFacade.TimeBilling(identityNo, time, lastMoney);//将当前已经上机登录用户的已用时间传值至外观层

                    double rate;   //定义费率
                    string PriceId = Convert.ToString(TestHelpCommon.PriceId);
                    List<Entity.Customer> list2 = timeBillingFacade.GetTimeBillingBycboprice(PriceId);
                    //查询是否为会员
                    if (TestHelpCommon.CustomerType(identityNo))
                    {
                        rate = Convert.ToDouble(list2[0].VIPUsers);
                    }
                    else
                    {
                        rate = Convert.ToDouble(list2[0].OrdinaryUsers);
                    }

                    //判断余额是否充足
                    if (price % 6 == 0)   //每6分钟进行一次扣费,上机不满6分钟不计费
                    {
                        if (Convert.ToDouble(list[j].LastMoney) > 0)
                        {
                            double lastMoney = Convert.ToDouble(list[j].LastMoney) - rate / 10;
                            timeBillingFacade.TimeBilling(identityNo, time, lastMoney);//将当前已经上机登录用户的已用时间传值至外观层
                        }
                        else
                        {
                            //当余额不足时自动结账下机
                            double LastMoney = Convert.ToDouble(list[j].LastMoney);
                            TestHelpCommon.CheckOut(identityNo, LastMoney);
                        }
                    }
                }
            }
        }

The following is the information I found on the Internet.

IndexOf() 


Find the position of the first occurrence of the specified character or string in the string, and return to the index value, such as: 
str1.IndexOf("字"); //Find the index value (position) of "word" in str1 
str1.IndexOf(" String");//Find the index value (position) of the first character of "string" in str1 
str1.IndexOf("word",start,end);//From the start+1 character of str1 , Find end characters, find the position of "word" in the string STR1 [counting from the first character] Note: start+end cannot be greater than the length of str1


The indexof parameter is string, find the position of the first occurrence of the parameter string in the string and return that position. Such as string s="0123dfdfdf";int i=s.indexof("df"); At this time, i==4. 
If you need a more powerful string parsing function, you should use the Regex class, and use regular expressions to match strings.

 
indexof(): Position characters and strings from front to back in the string; all return values ​​refer to the absolute position of the string, if it is empty, it is -1

string test="asdfjsdfjgkfasdsfsgfhgjgfjgdddd";

   test.indexof('d') = 2 //Locate the first occurrence of d from front to back
   test.indexof('d',1) = 2 //Locate d from front to back, first from the third string The position of the second occurrence
   test.indexof('d',5,2) =6 //Locate from front to back d Check from the 5th digit, check 2 digits, that is, from the 5th to the 7th;

lastindexof(): Position characters and strings from back to front in the string; the
usage is exactly the same as indexof().

 

The following is the console program that I typed when I learned to understand

====================================================================


IndexOfAny ||lastindexofany

They accept a character array as an argument, other methods are the same as above, and return the index position of the earliest occurrence of any character in the array

as follows

         char[] bbv={’s’,’c’,’b’};
         string abc = "acsdfgdfgchacscdsad";
        
         Response.Write(abc.IndexOfAny(bbv))=1
         Response.Write(abc.IndexOfAny(bbv, 5))=9
         Response.Write(abc.IndexOfAny(bbv, 5, 3))=9

lastindexofany 同上。
====================================================================


substring() usage

 

string a="aadsfdjkfgklfdglfd"

a.substring(5) //Intercept all strings after the fifth

a.substring(0,5) //Intercept all strings from 0 to 5

The following is the console program that I typed when I learned to understand

Guess you like

Origin blog.csdn.net/weixin_44684272/article/details/106984635