Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
Zoltán Zörgő 3-Oct-12 2:02am    
And what have you tried so far?
Charles Shob 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ő 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

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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900