首页 > Windows开发 > 详细

c#中字符串截取使用的方法

时间:2021-05-31 15:57:56      阅读:21      评论:0      收藏:0      [点我收藏+]
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 "...";
         }
     }

c#中字符串截取使用的方法

原文:https://blog.51cto.com/anlaoliu/2835242

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!