C#截取字串(String.Substring方法)

來源:互聯網
上載者:User

String.Substring 方法

名稱 說明
String.Substring (Int32)         從此執行個體檢索子字串。子字串從指定的字元位置開始。
String.Substring (Int32, Int32) 從此執行個體檢索子字串。子字串從指定的字元位置開始且具有指定的長度。

舉例如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Hello C# World!";
            //s1為從s中截取的位置為3的字元以後的字元子串,3表示子字串的起始字元位置
            string s1=s.Substring(3);
            Console.WriteLine(s1);
            //s2為從s中截取的位置為6的字元開始長度為2的字串,6表示子字元的起始字元位置,2表示子字元長度
            string s2 = s.Substring(6, 2);
            Console.WriteLine(s2);
        }
    }
}
結果如下:

lo C# World!

C#

隨即在附上一個C#截取字串函數

public string getString(string RawString, Int32 Length)
     {
         if (RawString.Length <= Length)
         {
             return RawString;
         }
         else
         {
             for (Int32 i = RawString.Length - 1; i >= 0; i--)
             {
                 if (System.Text.Encoding.GetEncoding("GB2312").GetByteCount(RawString.Substring(0, i)) < Length)
                 {
                     return RawString.Substring(0, i) + "...";
                 }
             }
             return "...";
         }
     }

 

1/**//// <summary>
2        /// 截取字串,不限制字串長度
3        /// </summary>
4        /// <param name="str">待截取的字串</param>
5        /// <param name="len">每行的長度,多於這個長度自動換行</param>
6        /// <returns></returns>
7        public string CutStr(string str,int len)
8        {    string s="";
9            
10            for(int i=0;i<str.Length ;i++)
11            {
12                int r= i% len;
13                int last =(str.Length/len)*len;
14                if (i!=0 && i<=last)
15                {
16                    
17                    if( r==0)
18                    {
19                        s+=str.Substring(i-len,len)+"<br>";
20                    }
21                        
22                }
23                else if (i>last)
24                {
25                    s+=str.Substring(i-1) ;
26                    break;
27                }
28                
29            }
30            
31            return s;
32            
33        }
34
35
36        /**//// <summary>
37        /// 截取字串並限制字串長度,多於給定的長度+。。。
38        /// </summary>
39        /// <param name="str">待截取的字串</param>
40        /// <param name="len">每行的長度,多於這個長度自動換行</param>
41        /// <param name="max">輸出字串最大的長度</param>
42        /// <returns></returns>
43        public string CutStr(string str,int len,int max)
44        {
45            string s="";
46            string sheng="";
47            if (str.Length >max)
48            {
49                str=str.Substring(0,max) ;
50                sheng="";
51            }
52            for(int i=0;i<str.Length ;i++)
53            {
54                int r= i% len;
55                int last =(str.Length/len)*len;
56                if (i!=0 && i<=last)
57                {
58                    
59                    if( r==0)
60                    {
61                        s+=str.Substring(i-len,len)+"<br>";
62                    }
63                        
64                }
65                else if (i>last)
66                {
67                    s+=str.Substring(i-1) ;
68                    break;
69                }
70                
71            }
72            
73            return s+sheng;
74            
75        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.