Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-08-24
10
Medium Priority
?
290 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 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