Variation on string manipulation
-
Here is an example:- "string of 23 characters" The above string has 23 characters, including the spaces What I want to do is count the number of charactersand then split the string at the next space after the half number, in this case, half of 23 is rounded up to 12 so I need to split the string between the "3" and the "c" to read like this:- "string of 23" and "characters" I've tried various string functions, and combinations of them but cannot work out how to do it. I'd appreciate your help. Regards, Steve
Thanks for your help and advice! Steve
-
Here is an example:- "string of 23 characters" The above string has 23 characters, including the spaces What I want to do is count the number of charactersand then split the string at the next space after the half number, in this case, half of 23 is rounded up to 12 so I need to split the string between the "3" and the "c" to read like this:- "string of 23" and "characters" I've tried various string functions, and combinations of them but cannot work out how to do it. I'd appreciate your help. Regards, Steve
Thanks for your help and advice! Steve
Hi, I'm not going to give you any solution; I do suggest you try and solve this little task in several steps, it is a typical beginner's mistake to try and solve it in one complex statement (which often never gets composed correctly). With many statements, you can execute the required steps, similar to your explanation in English words, AND you can check intermediate results. So don't worry if you use ten lines of code for this; once you got it working correctly, you can try and optimize it. One hint: you don't need loops, a few simple string methods will do. :)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Here is an example:- "string of 23 characters" The above string has 23 characters, including the spaces What I want to do is count the number of charactersand then split the string at the next space after the half number, in this case, half of 23 is rounded up to 12 so I need to split the string between the "3" and the "c" to read like this:- "string of 23" and "characters" I've tried various string functions, and combinations of them but cannot work out how to do it. I'd appreciate your help. Regards, Steve
Thanks for your help and advice! Steve
You will could use the following methods of your string variable, to achieve your requirements :-
.Length
.Substring
.Replacelike so:-
Dim FullString = "string of 23 characters"
Dim FirstPart As String
Dim SecondPart As StringFirstPart = FullString
SecondPart = FullString.Substring(FullString.Length /2)
FirstPart = Replace(FirstPart, SecondPart, "")Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
-
Here is an example:- "string of 23 characters" The above string has 23 characters, including the spaces What I want to do is count the number of charactersand then split the string at the next space after the half number, in this case, half of 23 is rounded up to 12 so I need to split the string between the "3" and the "c" to read like this:- "string of 23" and "characters" I've tried various string functions, and combinations of them but cannot work out how to do it. I'd appreciate your help. Regards, Steve
Thanks for your help and advice! Steve
I think is this the code you need:
Dim FullString As String = "String of 23 characters"
Dim HalfStringLen As Integer = Fix(FullString.Length / 2)
Dim FirstPart As String = FullString
Dim SecondPart As String = ""
Do
If FullString(HalfStringLen) = " " Then
FirstPart = Microsoft.VisualBasic.Left(FullString, HalfStringLen)
SecondPart = LTrim(Microsoft.VisualBasic.Right(FullString, FullString.Length - HalfStringLen))
Exit Do
End If
HalfStringLen = HalfStringLen + 1
Loop Until HalfStringLen = FullString.LengthAt end of loop, FirstPart and SecondPart will contain FullString spitted. Note that If the full string have not espaces, FirstPart will contain the entire FullString. Ignazio
-
I think is this the code you need:
Dim FullString As String = "String of 23 characters"
Dim HalfStringLen As Integer = Fix(FullString.Length / 2)
Dim FirstPart As String = FullString
Dim SecondPart As String = ""
Do
If FullString(HalfStringLen) = " " Then
FirstPart = Microsoft.VisualBasic.Left(FullString, HalfStringLen)
SecondPart = LTrim(Microsoft.VisualBasic.Right(FullString, FullString.Length - HalfStringLen))
Exit Do
End If
HalfStringLen = HalfStringLen + 1
Loop Until HalfStringLen = FullString.LengthAt end of loop, FirstPart and SecondPart will contain FullString spitted. Note that If the full string have not espaces, FirstPart will contain the entire FullString. Ignazio
-
Hi Ignazio and Steve, Thanks for your replies, they have both helped me a lot and it's helped me to solve my problem. again, thanks Steve
Thanks for your help and advice! Steve
Congratulations! You managed to con someone into doing your work for you! Don't do it again.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...