Hi,
I wonder how I can split a string in the middle, by the closest whitespace, so that it returns only 2 strings. This secures that the string does not split a word.
Example:
The text is “Split this string”
I want it to output either:
“Split this” and “string”,
or
“Split” and “this string”
As long as it does not output more than 2 strings it is the same what it outputs of the above mentioned.
I have tried to use both the Split- and substring functions without getting the desired output.
@skandi, what you’re looking for is “strToSplit.Split(New Char() {” “c}, 2)”.
Here’s a little workflow that splits “Now is the time for all good men” into “Now” and “is the time for all good men”.
Regards,
burque505
But is there a way to split by the space closest to the middle of the sentence? Or split by the the third space? And still return only two strings.
I have a text field in a form which can only take 50 characters. So If the sentence contains more than 50 characters I would have to split the sentence into two strings, and enter the second string in the text field below.
If I split by the first space It would be a possibility that the second string still contains more than 50 characters, so that is why I would split it as close to the middle as possible.
@skandi, there are many ways to do what you describe.
I’m going to give you several links. Strings in C# and VB.Net part 1, Strings in C# and VB.NET part 2, and Strings in VB.NET. I strongly suggest you look at MSDN - Strings, especially the section at String Methods.
I’m reluctant to offer a workflow just yet, since I’m wondering what will happen if your sentence is longer than 100 characters. Will you have to split it again? What if the first part of the sentence is “I’m a big fan of the […]”? That wipes out your three-space requirement way before the middle.
You may want to consider using regular expressions (Import System.Text.RegularExpressions).
Regards,
burque505
Try using this with “Invoke Code Activity” with input string and maximum characters you want in each line, this will return splitted strings.
Dim words As List(Of String) = strInput.Split(" "c).ToList()
If strInput.Length < maxCharacters OrElse words.Count = 1 Then
strOutput = strInput
Else
Dim lines As New List(Of String)
Dim currentLine As String = words(0)
For i As Integer = 1 To words.Count - 1
If (currentLine & " " & words(i)).Length > maxCharacters Then
lines.Add(currentLine)
currentLine = words(i)
If i = words.Count - 1 Then
lines.Add(currentLine)
End If
Else
If i = words.Count - 1 Then
lines.Add(currentLine & " " & words(i))
End If
currentLine &= " " & words(i)
End If
Next
strOutput= String.Join(Environment.NewLine, lines.ToArray())
why not you guys split based on spaces and write it to array, after that check the array length and write it to strings based on it. As example if array length is 6 then write first 3 array elements to one string and remaining to other, let me know if it helps