Solved

Easy question. syntax.

Posted on 2008-06-19
8
346 Views
Last Modified: 2013-12-16
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
Comment
Question by:mathieu_cupryk
8 Comments
 

Author Comment

by:mathieu_cupryk
ID: 21827116
I should trim the after spaces?
0
 

Author Comment

by:mathieu_cupryk
ID: 21827124
'NO.2 CANADA WESTERN AMBER DURUM (15.1)                                                   '

trim the rest of the spaces.

I don't think trim 80 will work.
0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
ID: 21827215
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 18

Expert Comment

by:Richard Lee
ID: 21827224
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21827226
use SubString:

gradeName = cellGradeValue.Value2.ToString().Substring(0, 80);
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21827432
Are the single quotes part of the string?...
0
 
LVL 9

Expert Comment

by:Rahul Goel ITIL
ID: 21828608
This will work for you

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

Open in new window

0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 21829210
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question