Solved

How do you TRIM n characters from a string from the right.

Posted on 2010-08-24
10
285 Views
Last Modified: 2013-12-17
I should trim last name of a employee to 18 characters even though if I get a 30 character last name from the database.
What is the best way to do that ? any help would be greatly appreciated.

Thanks
0
Comment
Question by:prashant-Vanam
  • 4
  • 4
  • 2
10 Comments
 
LVL 9

Expert Comment

by:wellhole
ID: 33512257
lastname = lastname.substring(0, 18) // cut name starting from 0 for 18 characters
0
 
LVL 14

Expert Comment

by:systan
ID: 33512312
Should this be;
text = text.substring(19, 30);
0
 
LVL 14

Expert Comment

by:systan
ID: 33512356
Oh, I have it wrong, wellhole got it.
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 14

Expert Comment

by:systan
ID: 33512389
He got it but with wrong comment, that's why I reverse it, but I got it wrong it code instead.

>>// cut name starting from 0 for 18 characters
// cut name starting from 19 to the last characters, instead.
0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33512399
str = str.Substring(0, 18);
str=str.Remove(18);

which one is better to use Substring or Remove
0
 
LVL 14

Expert Comment

by:systan
ID: 33512528
str=str.Remove(18);

substring uses remove
0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33512827
Substring and remove throws exception if the length of the string is less than 18 characters.
0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33512867
This is my code ...I'm wring to a fixed length text file and I need to make sure that the length of the string doesnt break the pattern of fixed length text file. That means the length of the lastname should never be more than 18 characters. If it is more than 18 characters in the DB then I should truncate it to 18 characters.
using (var sw = new StreamWriter(txtFile))
            {
                foreach (Employee employee in lstEmployees)
                    sw.WriteLine(String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}",
                                               employee.Ssn.Substring(0,9).PadRight(9),
                                               employee.EmployeeLastName.Substring(0,18).PadRight(18),
                                               employee.EmployeeFirstName.Remove(12).PadRight(11),
                                               employee.EmployeeMiddleName.Remove(1).PadRight(2),
                                               employee.EmployeeAddress1.Remove(46).PadRight(46),
                                               employee.EmployeeCity.Remove(16).PadRight(16),
                                               employee.EmployeeState.Remove(2).PadRight(2),
                                               employee.EmployeeZipCode.Remove(5).PadRight(5),
                                               employee.EmployeeZipCode4.Remove(21).PadRight(21),
                                               (string.IsNullOrEmpty(employee.HireDate) ? string.Empty : String.Format("{0:yyyyMMdd}", Convert.ToDateTime(employee.HireDate))).Remove(8).PadRight(8),
                                               (string.IsNullOrEmpty(employee.BirthDate) ? string.Empty : String.Format("{0:yyyyMMdd}", Convert.ToDateTime(employee.BirthDate))).Remove(8).PadRight(9),
                                                employee.CompanyName.Remove(30).PadRight(30),
                                                employee.CompanyAddress1.Remove(46).PadRight(46),
                                                employee.CompanyCity.Remove(16).PadRight(16),
                                                employee.CompanyState.Remove(2).PadRight(2),
                                                employee.CompanyZipCode.Remove(5).PadRight(5),
                                                employee.CompanyZipCode4.Remove(21).PadRight(21),
                                                employee.FederalId.Remove(52).PadRight(52),
                                                State.PlaceHolder.Remove(1).PadRight(1)));

            }

Open in new window

0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33512998
I will do this in the worst case........please let me know if anyone has any better solution.

Thanks
using (var sw = new StreamWriter(txtFile))
            {
                foreach (Employee employee in lstEmployees)
                    sw.WriteLine(String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}",
                        (employee.Ssn.Length > 9 ? employee.Ssn.Substring(0, 9).PadRight(9) : employee.Ssn.PadRight(9)),
                        (employee.EmployeeLastName.Length > 18 ? employee.EmployeeLastName.Substring(0, 18).PadRight(18) : employee.EmployeeLastName.PadRight(18)),
                            }
}

Open in new window

0
 
LVL 9

Accepted Solution

by:
wellhole earned 500 total points
ID: 33513442
That's actually among the best ways to do it. You can remove the PadRight from the Substring.PadRight because you're already guaranteed 18 length.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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