Split string by whitespace

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.

The text is “Split this string”

I want it to output either:
“Split this” and “string”,
“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.

Any help would be appreciated :slight_smile:

@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”.

SplitToTwoStringsOnSpace.xaml (7.6 KB)

1 Like

Thank you @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).

1 Like

Thank you @burque505, I will check out those links!

Did you find a solution?
this is exactly my requirement too…

Please try to use Start Index and End Index and then find the SubString that you want.

See this- Substring a string with each space found - #6 by ddpadil


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
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
currentLine = words(i)

    If i = words.Count - 1 Then
    End If
    If i = words.Count - 1 Then
      lines.Add(currentLine & " " & words(i))
    End If

    currentLine &= " " & words(i)
  End If

strOutput= String.Join(Environment.NewLine, lines.ToArray())

End If

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

hi @Badagala_Jagadeesh

Please check out the attached sample xaml file.
Hope that helps meeting your requirement :slight_smile:

Pramilasplit_text.xaml (6.0 KB)

it is solved with this solution … Split string by length - #3 by Sasi.lalo