代码之家  ›  专栏  ›  技术社区  ›  Josh

.Net等效于旧的vb左(字符串,长度)函数?

  •  63
  • Josh  · 技术社区  · 17 年前

    作为一名非.NET程序员,我正在寻找旧的Visual Basic函数的.NET等价物 left(string, length) . 它是懒惰的,因为它适用于任何长度的字符串。果然,, left("foobar", 3) = "foo" 而最有帮助的是,, left("f", 3) = "f"

    在.NET中 string.Substring(index, length)


    @诺多林 -哇,谢谢你的VB.NET扩展!我的第一次邂逅,虽然在C#中我花了几秒钟才做到这一点:

    public static class Utils
    {
        public static string Left(this string str, int length)
        {
            return str.Substring(0, Math.Min(length, str.Length));
        }
    }
    

    this "foobar".Left(3) . 另见 C# extensions on MSDN .

    11 回复  |  直到 6 年前
        1
  •  57
  •   Patrick J Collins    13 年前

    这里有一个扩展方法可以完成这项工作。

    <System.Runtime.CompilerServices.Extension()> _
    Public Function Left(ByVal str As String, ByVal length As Integer) As String
        Return str.Substring(0, Math.Min(str.Length, length))
    End Function
    

    这意味着您可以像旧的VB一样使用它 Left 功能(即。 Left("foobar", 3) )或者使用更新的VB.NET语法,即。

    Dim foo = "f".Left(3) ' foo = "f"
    Dim bar = "bar123".Left(3) ' bar = "bar"
    
        2
  •  37
  •   Oded    17 年前

    另一个单行选项如下所示:

    myString.Substring(0, Math.Min(length, myString.Length))
    

        3
  •  34
  •   Garry Shutler    17 年前

    Strings.Left 那是 确切地 同样的方法。

        4
  •  14
  •   Peter Mortensen Pieter Jan Bonestroo    6 年前

    public static string Left(this string str, int count)
    {
        if (string.IsNullOrEmpty(str) || count < 1)
            return string.Empty;
        else
            return str.Substring(0,Math.Min(count, str.Length));
    }
    
        5
  •  6
  •   Peter Mortensen Pieter Jan Bonestroo    6 年前

    使用:

    using System;
    
    public static class DataTypeExtensions
    {
        #region Methods
    
        public static string Left(this string str, int length)
        {
            str = (str ?? string.Empty);
            return str.Substring(0, Math.Min(length, str.Length));
        }
    
        public static string Right(this string str, int length)
        {
            str = (str ?? string.Empty);
            return (str.Length >= length)
                ? str.Substring(str.Length - length, length)
                : str;
        }
    
        #endregion
    }
    

    它不应该出错,返回空字符串形式的null,并返回修剪过的值或基值。像“testx.Left(4)或str.Right(12)”一样使用它;

        6
  •  5
  •   Peter Mortensen Pieter Jan Bonestroo    6 年前

    private string left(string inString, int inInt)
    {
        if (inInt > inString.Length)
            inInt = inString.Length;
        return inString.Substring(0, inInt);
    }
    

        7
  •  2
  •   RobS    17 年前

    您可以将对子字符串的调用封装在一个新函数中,按照其他答案中的建议测试其长度(正确的方法),或者使用Microsoft.VisualBasic命名空间并直接使用left(通常认为是错误的方法!)

        8
  •  2
  •   Peter Mortensen Pieter Jan Bonestroo    6 年前

    另一种技术是通过添加Left()方法来扩展string对象。

    以下是有关此技术的源文章:

    http://msdn.microsoft.com/en-us/library/bb384936.aspx

    Module StringExtensions
    
        <Extension()>
        Public Function Left(ByVal aString As String, Length As Integer)
            Return aString.Substring(0, Math.Min(aString.Length, Length))
        End Function
    
    End Module
    

    然后将其放在要使用扩展名的任何文件的顶部:

    Imports MyProjectName.StringExtensions
    

    像这样使用它:

    MyVar = MyString.Left(30)
    
        9
  •  1
  •   danfolkes    12 年前

    我喜欢这样做:

    string s = "hello how are you";
    s = s.PadRight(30).Substring(0,30).Trim(); //"hello how are you"
    s = s.PadRight(3).Substring(0,3).Trim(); //"hel"
    

    不过,如果你想要尾随空格或起始空格,那你就倒霉了。

        10
  •  0
  •   LarsTech    8 年前

    如果要避免使用扩展方法并防止长度不足错误,请尝试以下操作

    string partial_string = text.Substring(0, Math.Min(15, text.Length)) 
    // example of 15 character max
    
        11
  •  0
  •   Peter Mortensen Pieter Jan Bonestroo    6 年前

    只是在一个非常特殊的情况下:

    如果在左侧执行此操作,您将使用部分字符串检查数据,例如:

    if(Strings.Left(str, 1)=="*") ...;

    StartsWith EndsWith

    if(str.StartsWith("*"))...;

    推荐文章