Click here to Skip to main content
16,019,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text box and there are some string in it that could be anything and I need to split that string into every 38 characters But the code I use does not work properly Because sometimes after 38 characters it is possible to create the correct separation on a word like:
for example, my string is:
"Well, my house is very cluttered...very cluttered. Just things that were left behind from other generations and some of my mom's things and my granddad's things. Also things that I've accumulated over the years. I definitely have concerns about when Joaquin starts walking that, you know, he could hurt himself, like, get into something or, like, pull something down."

and after separation:
Well, my house is very cluttered...ver
y cluttered. Just things that were lef
t behind from other generations and so
me of my mom's things and my granddad'
s things. Also things that I've accumu
lated over the years. I definitely hav
e concerns about when Joaquin starts w
alking that, you know, he could hurt h
imself, like, get into something or, l


And as you can see, very, left, Walking, have been separated And the sentence sequence has not been added (like, pull something down.").
Is there a code that does this separation intelligently, that is, it separates 36 characters, and if these 36 characters were in the middle of a word, it separates even less, That is not a problem like

Quote:
Well, my house is very
cluttered...very cluttered. Just
things that were left behind from
other generations and some of my
mom's things and my granddad's
things. Also things that I've
accumulated over the years. I
definitely have concerns about
when Joaquin starts walking that,
you know, he could hurt himself,
like, get into something or,
like, pull something down.


The code I use is very simple and basic.
Please show me the code if there is a solution to this problem. Thank you very much

What I have tried:

Dim s = TextBox1.Text
       Dim list = Enumerable.Range(0, s.Length \ 38).Select(Function(i) s.Substring(i * 38, 38))
       Dim res = String.Join(vbCrLf, list)
       TextBox1.Text = res
Posted
Updated 29-Jun-22 22:51pm

1 solution

There is no "standard method" that will do that for you, but it's not too complex.
Start by finding out what the 38th character is: letter or punctuation.
If it's a letter, look back to find the first punctuation character and split the string there.
Otherwise, split it at the 38th character.
Repeat!

Remember to check that the string contains at least 38 characters before you try to check ... :D

And see here: String.Substring Method (System) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 12617947 30-Jun-22 8:28am    
Dear OriginalGriff, It was great, thank you, this is exactly what I wanted
OriginalGriff 30-Jun-22 8:41am    
You're welcome!

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