Split string by whitespace


#1

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.

Any help would be appreciated :slight_smile:


#2

@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

SplitToTwoStringsOnSpace.xaml (7.6 KB)


#3

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.


#4

@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


#5

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


#6

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


#7

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


#8

Hi,

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())

End If


#9

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


#10

hi @Badagala_Jagadeesh

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

Regards,
Pramilasplit_text.xaml (6.0 KB)


#11

it is solved with this solution … Split string by length