Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello,
 
I need help from the following string operation.
 
I am having some string with a length of about 300 chars for instance. I need to split this into equal lines. I mean to say i should show it in a UI label that should fit it's length.
 
if length of source string is > 50 and the index of 50 is not equal to space then a new line should be added at this point. If index at 50 is not space then, it should move to next space and split. Like wise it should insert new line at the specified [50] length [50,100,150,200,250,300 .. etc].
 
Appologies if confused. Kindly help. Thanks in advance.
 
Regards,
chowdary.
Posted 2-Oct-12 20:00pm
Comments
Zoltán Zörgő at 3-Oct-12 2:02am
   
And what have you tried so far?
chowdary2005 at 3-Oct-12 2:17am
   
Really i am not getting an idea. Tried with arrays, strig lengh with for loop etc. but no luck.
Zoltán Zörgő at 3-Oct-12 2:23am
   
Parse the string and use a StringBuilder to construct a new one with the proper newlines. But you have to figure out the algorithm. Try first to solve the problem on paper (without any coding), yus to see how you as "computer" could solve this, and than you can transform your own logic to code.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Look at the string.IndexOf method - it has an overload which starts at a specified character position: http://msdn.microsoft.com/en-us/library/5xkyx09y.aspx[^]
 
Sorry, but there is no built-in way to do that so you will have to re-discover the algorithm for yourself!
 
And it's not going to be as easy as you think - most Windows fonts are what is called proportionally spaced which means that an 'i' is a lot narrower than a 'w' and so on - so the "length" of a string in the display is not so directly related to the number of characters as you think!
 
From a visual POV, you may get a better result if you split the input string into words, then work on combining them back together while using Graphics.MeasureString[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 478
1 Sergey Alexandrovich Kryukov 305
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Uma Shankar Patel 80
0 Sergey Alexandrovich Kryukov 6,557
1 OriginalGriff 5,966
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100