Solved

Easy question. syntax.

Posted on 2008-06-19
8
344 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now