Lordlebu
Lordlebu

Reputation: 429

Substring is not working as expected if length is greater than length of String

I know if/else works, but I needed an alternative.

I am using

B = String.Concat(A.Substring(0, 40));

to capture the first 40 characters of a value.

If the value at A is more than 40, B is able to capture, but if the value of A is less than 40, there is no value being captured at B.

Upvotes: 32

Views: 75664

Answers (9)

Me.Name
Me.Name

Reputation: 12544

A quick one line would be:

  B = A?.Length > 40 ? A.Substring(0, 40) : A;

which only implements the substring when the length is more than 40. For the sake of redundancy, 40 would preferably be a variable of course. The use of '?.' prevents errors when 'A' is null. As ean5533 mentioned A.Substring(0, Math.Min(40, A.Length)) can also be used. Same outcome, prevents the use of '40' twice, but will always invoke the substring function (not that that matters in this day of age)

For ease of use, an extension method can be created

public static string Truncate(this string value, int MaxLength) => value?.Length > MaxLength? value.Substring(0, MaxLength) : value;

Upvotes: 78

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

Just use:

var B = A.Substring(0, Math.Min(A.Length, 40));

Upvotes: 0

Philip
Philip

Reputation: 231

use below code to substring

B = String.padright(40).Substring(0, 40))

Upvotes: 23

Jacob Sobus
Jacob Sobus

Reputation: 981

Extensions are best for problems like this one ;)

Mine have some dirty name, but everyone knows what it would do - this is an exception-safe substring:

public static string SubstringNoLongerThanSource(this string source, int startIndex, int maxLength)
{
    return source.Substring(startIndex, Math.Min(source.Length - startIndex, maxLength));
}

Upvotes: 19

Ankita Sen
Ankita Sen

Reputation: 434

String.Concat does not serve your purpose here. You should rather do the following:

if(A.Length > 40)
    B = A.Substring(0, 40);
else
    B = A;

Upvotes: 21

Chris Gessler
Chris Gessler

Reputation: 23123

Create an extension for it... Call it Truncate or Left, or whatever.

public static class MyExtensions
{
    public static string Truncate(this string s, int length)
    {
        if(s.Length > length) 
            return s.Substring(0, length);
        return s;
    }
}

Then you can simply call it like so:

string B = A.Truncate(40);

Also note that you don't have to make it an extension method, although it would be cleaner.

In your StringTool class:

public static string Truncate(string value, int length)
{
    if(value.Length > length) 
        return value.Substring(0, length);
    return value;
}

And to call it:

string B = StringTool.Truncate(A, 40);

Upvotes: 30

sloth
sloth

Reputation: 101122

You can use Left from Microsoft.VisualBasic.Strings.

B = Microsoft.VisualBasic.Strings.Left(A, 40);

I don't know why you want to use Concat, anyway.

Upvotes: 4

AlexanderT
AlexanderT

Reputation: 89

Another method using Take

B = new string(A.Take(40).ToArray())

Or extension method

public static string Left(this string value, int count)
{
    return new string(value.Take(count).ToArray());
}

Upvotes: 5

irfandar
irfandar

Reputation: 1810

B = string.Concat(A.Substring(0, Math.Min(40, A.Length)));

Upvotes: 9

Related Questions