Solved

Easy question. syntax.

Posted on 2008-06-19
8
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 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