Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

Easy question. syntax.

I need to do a trim of 80.
How can I do this it is not working on this.
gradeName = cellGradeValue.Value2.ToString().Trim(80);


Error      1      The best overloaded method match for 'string.Trim(params char[])' has some invalid arguments      C:\SvnWork\InitialPriceReporting\DotNet\InitialPriceReporting\Form1.cs      1027      37      InitialPriceReporting
Error      2      Argument '1': cannot convert from 'int' to 'char[]'      C:\SvnWork\InitialPriceReporting\DotNet\InitialPriceReporting\Form1.cs      1027      75      InitialPriceReporting
Avatar of mathieu_cupryk
mathieu_cupryk
Flag of Canada image

ASKER

I should trim the after spaces?
'NO.2 CANADA WESTERN AMBER DURUM (15.1)                                                   '

trim the rest of the spaces.

I don't think trim 80 will work.
ASKER CERTIFIED SOLUTION
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here we go again. I replaced the literal number 80 with the parameter length. This should solve your problem.
/// <summary>
/// Removes the trailing characters to the number
/// specified by <paramref name="length"/>.
/// </summary>
public string TruncateString(string s, int length)
{
      int removeStartIndex = s.Length - length;
 
      if(removeStartIndex <= 0)
            return String.Empty;
 
      return s.Remove(removeStartIndex);
}

Open in new window

use SubString:

gradeName = cellGradeValue.Value2.ToString().Substring(0, 80);
Are the single quotes part of the string?...
This will work for you

cellGradeValue.Value2.ToString().Substring(0,80).Trim()

Open in new window

I'm having another after misunderstanding the question the first time. This should trim all spaces at the end of a string.
String s = "NO.2 CANADA WESTERN AMBER DURUM (15.1)                                                   ";
s.TrimEnd(new char[] { ' ' });

Open in new window