获取字符串的最后4个字符 [英] Get last 4 character of a string

查看:431
本文介绍了获取字符串的最后4个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让此字符串最后4个字符
字符串是Formandmanner.AAA
期望-C#中的.AAA

解决方案

你好

 字符串 text = " ;

字符串 myString =(text.Length >   3 )? text.Substring(text.Length- 4  4 ):文本;

MessageBox.Show(myString); 


try:

 字符串 temp = " ;
 string 输出= temp.Substring(temp.Length- 4  4 ); 


输出将具有.AAA值

您还可以为此编写扩展方法

 公共 静态 字符串正确( 字符串 s, int 长度)
        {
            长度= Math.Max(长度, 0 );

            如果(长度> 长度)
            {
                返回 s.Substring(s.Length-长度,长度);
            }
            其他
            {
                返回 s;
            }
        } 



然后你可以做

  var 结果= "  .Right( 4 ); 


i want to get this string last 4 character
string is Formandmanner.AAA
expectations-- .AAA in C#

解决方案

Hello

string text = "abcdefghijklmn";

string myString = (text.Length > 3)? text.Substring(text.Length - 4, 4): text;

MessageBox.Show(myString);


try:

string temp = "Formandmanner.AAA";
string output = temp.Substring(temp.Length - 4, 4);


output will have .AAA value


You can also write an extension method for this

public static string Right(this string s, int length)
        {
            length = Math.Max(length, 0);

            if (s.Length > length)
            {
                return s.Substring(s.Length - length, length);
            }
            else
            {
                return s;
            }
        }



And then you can do

var result = "Formandmanner.AAA".Right(4);


这篇关于获取字符串的最后4个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆