1 Replies - 765 Views - Last Post: 07 November 2011 - 12:02 AM Rate Topic: -----

#1 odiepus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-November 11

How do i get the character length of each array object

Posted 06 November 2011 - 11:16 PM

Hello,

I have looked in reference books, and googled how to get the character length of each object in an array. The only thing I have found is .Length.

I have a three arrays; one with name of college courses, second has max enrollment and, third has actual enrollment. I need to print to console screen the name of courses, max enrollment and, actual enrollment in a column and row style. The only way I can think of doing this is by using .PadRight/Left(). But I need to know in advance which college course object has the longest name so all the info always lines up no matter the length of the object. Knowing the max length of the college course will allow me to input this value into the .PadRight() argument with an extra 10 spaces.

Console.Write(collegeCourse[i].PadRight(s + 10));
Console.Write(maxEnrollment[i].PadRight(10));
Console.WriteLine(actualEnrollment[i].PadLeft(5));
 


This is how I want to display the text (Of course this is simplified. I would add headings over each different element of the column and, it would loop however many times needed to print to console screen.) But, how do I find the max so that I can always assign it to the variable "s".

I have been at this since this afternoon, and I think I am just burned out, but I do believe that there is a way. Can someone just point me in the right direction.

Is This A Good Question/Topic? 0
  • +

Replies To: How do i get the character length of each array object

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5239
  • View blog
  • Posts: 14,035
  • Joined: 18-April 07

Re: How do i get the character length of each array object

Posted 07 November 2011 - 12:02 AM

The .Length property does give you the character length of a string...

// This returns 4, the number of characters in the string "test"
Console.WriteLine("test".length.ToString());



Now add this to a string.format call and you will get your width and needed padding...

// Writes "      test". That is a width of ten, minus 4 gives 6 padding.
// Use a negative number to get left alignment
Console.WriteLine(String.Format("{0,10}","test"));



Enjoy! :)
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1