.NET equivalent of old function vb left (string, length)?

As a non.net programmer, I am looking for the .net equivalent of the old vb left(string, length) function. This was lazy as it worked for any string length. As expected, left("foobar", 3) = "foo" while, most useful, left("f", 3) = "f" .

In .net string.Substring(index, length) throws exceptions for everything out of range. In Java, I always had Apache-Commons lang.StringUtils. At Google, I am not very good at string functions.

Edit:

@Noldorin - Wow, thanks for your vb.net extensions! My first meeting, although it took me a few seconds to do the same in C #:

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

Note the static class and method, as well as the this . Yes, they are as easy to call as "foobar".Left(3) . See Also C # Extensions in msdn .

+59
May 09 '09 at 9:01 p.m.
source share
10 answers

It uses an extension method that will do the job.

 <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 

This means that you can use it in the same way as the old VB Left function (ie Left("foobar", 3) ) or use the new VB.NET syntax, i.e.

 Dim foo = "f".Left(3) ' foo = "f" Dim bar = "bar123".Left(3) ' bar = "bar" 
+48
May 09, '09 at 21:10
source share

Add a link to the Microsoft.VisualBasic library, and you can use Strings.Left , which is exactly the same way.

+30
May 09 '09 at 9:19
source share

Another one line option would look something like this:

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

Where myString is the string you are trying to work with.

+28
May 09 '09 at 9:11 p.m.
source share

don't forget the zero case

  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)); } 
+11
Feb 11 2018-11-11T00:
source share

you can make your own

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

edit: mine is in C #, you have to change it for vb

+5
May 9 '09 at 9:05 p.m.
source share
 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 } 

Do not use an error, returns nulls as an empty string, returns cropped or base values. Use it as "testx" .Left (4) or str.Right (12);

+5
Jul 07 '14 at 21:00
source share

You can either wrap the substring call in a new function that checks its length, as suggested in other answers (correctly), or use the Microsoft.VisualBasic namespace and use the left link (usually considered incorrect!).

+1
May 09 '09 at 21:04
source share

Another way is to extend the string object by adding the Left () method.

Here is the source article on this technique:

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

Here is my implementation (in VB):

StringExtensions Module

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

End module

Then put this at the top of any file in which you want to use the extension:

 Imports MyProjectName.StringExtensions 

Use it as follows:

 MyVar = MyString.Left(30) 
+1
Sep 18 '12 at 16:27
source share

I like to do something like this:

 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" 

Although, if you need trailing or trailing spaces, you're out of luck.

I really like using Math.Min, this is the best solution.

+1
Mar 06 '14 at 14:51
source share

Only in a very special case:

If you do this like this, then you will check the data with some partial string, for example: if(Strings.Left(str, 1)=="*") ...;

Then you can also use C # instance methods such as StartsWith and EndsWith to complete these tasks. if(str.StartsWith("*"))...;

0
Sep 03 '17 at 7:17
source share



All Articles