Yahoo Clever wird am 4. Mai 2021 (Eastern Time, Zeitzone US-Ostküste) eingestellt. Ab dem 20. April 2021 (Eastern Time) ist die Website von Yahoo Clever nur noch im reinen Lesemodus verfügbar. Andere Yahoo Produkte oder Dienste oder Ihr Yahoo Account sind von diesen Änderungen nicht betroffen. Auf dieser Hilfeseite finden Sie weitere Informationen zur Einstellung von Yahoo Clever und dazu, wie Sie Ihre Daten herunterladen.

C#: How do you split a string after a specific length and after a whitespace?

This might be a really dumb question for some of you, but I just don't seem to get this right: I want to divide a long string into some lines of text. Therefore I need to split it after a specific length. As the words should be complete in one line it should also take care of the whitespaces. And there's the problem!

This is my code:

for(x=0; x<Text.Length; x+=CharsInOneLine)

{

if( !(x+CharsInOneLine > Text.Length) )

{

StringArray[ x / CharsInOneLine ] = Text.Substring (x , CharsInOneLine);

}

}

else

[...]

This code correctly splits the string into parts. But of course it does not take care of the whitespaces. And: No, I can't use \r\n to create a new line. It does not work in this specific case.

Can anyone of you help?

1 Antwort

Relevanz
  • vor 1 Jahrzehnt
    Beste Antwort

    If your string can't be longer than say 80 characters, go to character 80 and if it isn't a space have your program check character 79 and keep going back until you have a space and then split the the string.

Haben Sie noch Fragen? Jetzt beantworten lassen.