Click here to Skip to main content
15,072,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code
string a="Bill Gates founded the software company";
if (a.Length > 20)
                        a = a.Insert(20, Environment.NewLine);

above code output is showing like this
"Bill Gates founded t
he software company"

but i want wrapping if index word not completed then move whole word into next line
i want output like this
"Bill Gates founded
the software company"

What I have tried:

string a="Bill Gates founded the software company";
if (a.Length > 20)
a = a.Insert(20, Environment.NewLine);
Updated 19-Feb-21 21:47pm

Think about what you need to do in four cases: of course you may choose not to handle certain cases

1. the index is to a white-space character.

2. the index is to a non-white-space character where the next character is a white-space character.

3. the index is to a non-white-space character where the previous character is a white-space character.

4. the index is to a white-space character with other white-space characters before it, or after it, or has white-space before and after it.

some issues you may need to address:

1. trimming extra white-space ... at the end of the first line, or the beginning of the second line ... after the insertion.

2. in .NET C# the line delimiter "Environment.NewLine" is composed of two characters.

3. error-handling, validation of input parameter values

Here is some code to help you get started with the first, simple, case:
static string defaultNewLine  = Environment.NewLine;

public static string InsertNewline(string str, int ndx, string nlstr = "")
    if (nlstr == "") nlstr = defaultNewLine;

    // add code to check if parameters are valid

    // is there a white-space char at the insert index ?
    if (char.IsWhiteSpace(str[ndx]))
        return str.Insert(ndx, nlstr);

    // think about why the start index is set to ndx + #2
    // find the index of the next white-space char

    for (int i = ndx  + 2; i < str.Length; i++)
        // left for you  to write
        // if the character at ndx is white-space then ???

        ndx = i;

    return "";
Naeem Shah 20-Feb-21 6:56am
i am accepting your answer but update your loop because your approach almost correct but need some changes in loop and thanks for sharing this its help me to achieve my goal
for (int i = ndx - 1; i < str.Length; i--)
ndx = ndx - 1;
// left for you to write
// if the character at ndx is white-space then ???
if (char.IsWhiteSpace(str[i]))
return str.Insert(ndx, nlstr);
//ndx = i;
BillWoodruff 20-Feb-21 7:20am
Salaam, Naeem, I deliberately left the code missing in the loop, as explained in the comments.

My goal is to try and help you start solving the problem yourself ... to teach ... not write the code for you.

Whether you accept the solution, or not, is up to you ... I'm am old man with enough reputation :)
Naeem Shah 20-Feb-21 8:23am
i accepted thanks for your kind answer
Patrice T 20-Feb-21 7:42am
and my +5 too :)
but i want wrapping if index word not completed then move whole word into next line

There is no function that will do the job for you.
Your code split insert a newline at fixed position when you want to do it at beginning of last word within range of that position.
The code should look like:
if (a.Length > 20) {
    pos= 0; // to remember position of last non alpha char found
    for (scan= 0; scan < 20; scan++) {
        if (char at index scan is not alpha) { // exact code is left to you as an exercise
            pos= scan;
    a = a.Insert(pos+1, Environment.NewLine);

Note that solution will depend on the exact usage you want:
- number of newlines (single of multiple)
- long word handling
Naeem Shah 20-Feb-21 7:05am
i never tried your answer but still up vote for your and thanks for your kind help
Patrice T 20-Feb-21 7:22am
Thank you
There's no method that's going to do it for you. You're going to have to write your own method to do it.

How would you do it on paper? How would you look for the spot to place the NewLine? Then you write code to do what you did on paper.
BillWoodruff 20-Feb-21 2:49am
Voted #1: this is not a solution; it's gratuitous advice. The OP is asking for help with a problem; you respond with a not so subtle "go do it yourself" as if the OP had asked you to write his code for him.
Dave Kreskowiak 20-Feb-21 10:57am
Well, when I have a minute or two to peruse CP while waiting for a process at work, I guess I'll just skip coming here because I don't have the time to completely flesh out an answer to your specifications.
Patrice T 20-Feb-21 3:49am
+5 I think the downvote is a little rude.
BillWoodruff 20-Feb-21 7:25am
Gosh, Patrice, you have expressed respect for my choice to downvote publicly several times in the past, but, now, you are a proxy for Dave ?
Patrice T 20-Feb-21 7:41am
I still like you have the courage of your opinion, and I agree that the solution is a little 'cheap', but the downvote seems a little rude to me, just that.
To add to what Dave says, remember that there things you need to check:
1) The input may need "breaking" multiple times to fit in your length. For example "Bill Gates founded the software company Microsoft in 1975" will not fit in two 20 characters strings.
2) A single word may exceed your length: there are seventeen 21 character words in the English language, five 22 character words, and so up, right up to 45: "Pneumonoultramicroscopicsilicovolcanoconiosis". There are also hyphenated words which can easily exceed 20 characters.

But there are two ways to do this:
1) Use string.Split to break the input into individual words, and then assemble strings which are all the right length to fit.
2) Use string indexing ("hello"[1] is 'e' for example) and string.Substring to extract "short" lines by working back from a non-whitespace character to the word start. You can then either use a StringBuilder to assemble your output, or a List to collect them, and then string.Join to build your final output.
BillWoodruff 20-Feb-21 3:24am
I was writing my response when you posted this. When I can fantasize we are on the same wavelength, I almost feel (temporarily) like I am not past my use-by date :)

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