zoukankan      html  css  js  c++  java
  • c#中字符串截取使用的方法

    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         }

  • 相关阅读:
    几个用于序列化的代码片段
    一个统计查询写的哥要哭了
    pb数据窗口数据输入的下拉选择效果
    PB代码块
    获取指定表的字段信息sql2005
    TSQL时间格式化显示
    Atitit 为什么互联网金融会得到高层的支持 面子工程战略 政绩战略 大事业战略 高层需要在意识形态创新全球,政绩工程 得到合法性。 银行有很多家,而且别的区域也有。。不独特。。但是支付
    Atitit 软件与互联网理论 attilax总结
    Atitit 软件与开发的未来趋势 attilax总结 1.1. Sdx软件重构世界 软件定义未来 1 1.2. 《软件和信息技术服务业发展规划(20162020年)》( 2 1.3. Iot物联
    Atitit it业界与软件界的定律 原则 准则 法则 效应
  • 原文地址:https://www.cnblogs.com/jys509/p/2021158.html
Copyright © 2011-2022 走看看