zoukankan      html  css  js  c++  java
  • 对字符串进行相同长度循环截取

    添加一个button1,添加一个ListBox1

    由于ListBox1不能对于字符串换行输出,

    于是,本人做了一个“循环截取的方法”

    #region 对字符串循环截取
    /// <summary>
    /// 对字符串循环截取
    /// </summary>
    /// <param name="str">被截取的字符串</param>
    /// <param name="Length">截取的长度</param>
    /// <returns></returns>
    private string[] CutString(string str,int Length)
    {


    int StrTotalLength = str.Length; //字符串的总长度
    int ArraySize = 0; //根据Length的长度截取的份数
    //被截取的字符串小于 截取的长度
    if (StrTotalLength < Length)
    {
    MessageBox.Show(
    "字符串的总长度小于要截取的长度,非法操作!");
    return null;
    //ArraySize = (StrTotalLength / Length);
    }
    else
    {
    //被截取的字符串的长度正好是整数倍
    if (StrTotalLength % Length == 0)
    {
    ArraySize
    = (StrTotalLength / Length);
    string[] a = new string[ArraySize];
    for (int i = 0; i < ArraySize; i++)
    {

    a[i]
    = str.Substring(i * Length, Length);

    }
    return a;
    }
    else
    {
    try
    {
    //加1快数组空间
    ArraySize = (StrTotalLength / Length) + 1;
    string[] a = new string[ArraySize];
    for (int i = 0; i < ArraySize; i++)
    {

    if (i != ArraySize - 1)
    {
    a[i]
    = str.Substring(i * Length , Length);

    }
    //对于不是整数倍StrTotalLength % Length取余的结果作为截取的长度
    else
    {
    a[i]
    = str.Substring(i * Length , StrTotalLength % Length);
    }

    }
    return a;
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    return null;
    }
    }

    }


    }
    #endregion

    #region 按钮
    private void button3_Click(object sender, EventArgs e)
    {
    //定义数组来接受方法传回来的数组,数组的长度不好设定。

           //string abc = "00001000020000300004000050000600007";
                //const int CL = 30;
                //string[] b = new string[30];
                //b = CutString(abc, CL);

                //int tiana = 0;
                //if (abc.Length % CL == 0)
                //{
                //    tiana = abc.Length / CL;
                //}
                //else
                //{
                //    tiana = abc.Length / CL + 1;
                //}
                //for (int i = 0; i < tiana; i++)
                //{
                //    listBox1.Items.Add(b[i]);

                //}

    //定义动态数组ArrayList来接受方法传回来的数组。数组长度很灵活
                string str = "00001000020000300004000050000600007";
                ArrayList al = new ArrayList();
                const int CL = 10; //被截取的长度
                al.AddRange(CutString(str, CL));
                int strCount = 0;
                if (str.Length % CL == 0)
                {
                    strCount = str.Length/CL;
                }
                else
                {
                    strCount = str.Length / CL+1;
                }
                for (int i = 0; i < strCount; i++)
                {
                    listBox1.Items.Add(al[i]);
                }

    }
    #endregion

    知识点:如何把一维数组赋值给动态数组ArrayList(注意:ArrayList只能存放一维数组)

               方法有3种:

         1.循环;

                2.ArrayList al = new ArrayList[数组引用];

          如:int[] a = new int[] { 1, 2, 3, 4 };
            ArrayList list
    =new ArrayList(a);

         3.用ArrayList的AddRange()方法;

          如:int[] a = new int[] { 1, 2, 3, 4 };
            ArrayList list
    =new ArrayList();
            list.AddRange(a);

            

  • 相关阅读:
    SQL批量更新
    使用PLSQL导入导出数据库
    Oracle 的Blob使用小结
    基于java的邮件服务器以及webmail的搭建
    Tomcat 系统架构与设计模式 【2】
    修改Oracle XE Listener 占用的1521、8080端口
    nls_lang pl/sql 设置编码
    oracle提高查询效率的解析
    Tomcat 系统架构与设计模式
    hql与sql的区别(转)
  • 原文地址:https://www.cnblogs.com/beeone/p/2099357.html
Copyright © 2011-2022 走看看