• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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
0
mathieu_cupryk
Asked:
mathieu_cupryk
1 Solution
 
mathieu_cuprykAuthor Commented:
I should trim the after spaces?
0
 
mathieu_cuprykAuthor Commented:
'NO.2 CANADA WESTERN AMBER DURUM (15.1)                                                   '

trim the rest of the spaces.

I don't think trim 80 will work.
0
 
Richard LeeSoftware EnthusiastCommented:
Here is a function that will do what you need:

/// <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 - 80;

      if(removeStartIndex <= 0)
            return String.Empty;

      return s.Remove(removeStartIndex);
}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Richard LeeSoftware EnthusiastCommented:
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

0
 
Jaime OlivaresSoftware ArchitectCommented:
use SubString:

gradeName = cellGradeValue.Value2.ToString().Substring(0, 80);
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Are the single quotes part of the string?...
0
 
Rahul Goel ITILSenior Consultant - DeloitteCommented:
This will work for you

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

Open in new window

0
 
Richard LeeSoftware EnthusiastCommented:
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

0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now