Solved

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

Posted on 2010-08-24
10
288 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
[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
  • 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
Independent Software Vendors: 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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