I have one string that has two words in it:
Cat Dog
How can I split these up so I get:
Str1 = Cat and Str2 = Dog
Please note this is for VB6.
Use the Split function.
Dim output() As String
output = Split("Cat Dog", " ")
Would produce
output(0) = Cat
output(1) = Dog
or like this:
For Each i In output
.... a even here
next
Related
How to access the whole elements hello and ahoy in Octave? Only the first character in each string is being printed.
octave:1> s = ["hello";"ahoy"]
s =
hello
ahoy
octave:2> s(1)
ans = h
octave:3> s(2)
ans = a
Use cell arrays instead.
octave:1> s = { 'hello'; 'ahoy' };
octave:2> s{1}
ans = hello
octave:3> s{2}
ans = ahoy
See https://octave.org/doc/v5.2.0/Cell-Arrays.html#Cell-Arrays
Check the size and type of s to understand what's going on:
octave:5> size(s)
ans =
2 5
octave:6> class(s)
ans = char
It's a 2x5 matrix of characters. To index, use matrix indexing. For example getting the first row:
octave:7> s(1,:)
ans = hello
i have problem to split string into newline in vb.net.
right now i can make it to split by a single space.i want split new line after 3 space.
Dim s As String = "SOMETHING BIGGER THAN YOUR DREAM"
Dim words As String() = s.Split(New Char() {" "c})
For Each word As String In words
Console.WriteLine(word)
Next
output :
SOMETHING
BIGGER
THAN
YOUR
DREAM
Desire output :
SOMETHING BIGGER THAN
YOUR DREAM
Another alternative added to existing efficient answers might to be:
Dim separator As Char = CChar(" ")
Dim sArr As String() = "SOMETHING BIGGER THAN YOUR DREAM".Split(separator)
Dim indexOfSplit As Integer = 3
Dim sFinal As String = Join(sArr.Take(indexOfSplit).ToArray, separator) & vbNewLine &
Join(sArr.Skip(indexOfSplit).ToArray, separator)
Console.WriteLine(sFinal)
You can split your input string, then loop the array of parts generated and add them to a StringBuilder object.
When you have read a number of parts that is multiple of a defined value, (wordsPerLine, here), you append vbNewLine to the current part.
When the loop completes, print the content of the StringBuilder to the Console:
Dim input As String = "SOMETHING BIGGER THAN YOUR DREAM, NOT MORE THAN YOUR ACCOUNT BALANCE"
Dim wordsPerLine As Integer = 3
Dim wordsCounter As Integer = 1
Dim sb As StringBuilder = New StringBuilder()
For Each word As String In input.Split()
sb.Append(word & If(wordsCounter Mod wordsPerLine = 0, vbNewLine, " "))
wordsCounter += 1
Next
Console.WriteLine(sb.ToString())
Prints:
SOMETHING BIGGER THAN
YOUR DREAM, NOT
MORE THAN YOUR
ACCOUNT BALANCE
Instead of using split, you might capture 3 words in a capturing group and match the trailing whitespace chars.
In the replacement use the group followed by a newline.
Pattern
(\S+(?:\s+\S+){2})\s*
That will match:
( Capture group 1
\S+ Match 1+ non whitespace chars
(?:\s+\S+){2} Repeat 2 times matching 1+ whitespace chars and 1+ non whitespace chars
) Close group 1
\s* Match trailing whitespace chars
.NET Regex demo | VB.NET demo
Example code
Dim s As String = "SOMETHING BIGGER THAN YOUR DREAM"
Dim output As String = Regex.Replace(s, "(\S+(?:\s+\S+){2})\s*", "$1" + Environment.NewLine)
Console.WriteLine(output)
Output
SOMETHING BIGGER THAN
YOUR DREAM
String.Join has an overload that will help you.
First parameter is the character to use between elements of your array.
Second parameter is the array you wish to join.
Third parameter is the starting position, for the first line in your desired output this would be the element at index 0.
Fourth parameter is the length to use, for the first line we want three array elements.
Private Sub OPCode()
Dim s As String = "SOMETHING BIGGER THAN YOUR DREAM"
Dim words As String() = s.Split(New Char() {" "c})
Dim line1 As String = String.Join(" ", words, 0, 3)
Console.WriteLine(line1)
Dim line2 As String = String.Join(" ", words, 3, words.Length - 3)
Console.WriteLine(line2)
End Sub
Given a string, for example,...
1-23-4567 Apple Sauce
I'm trying to figure out how to split that into
1-23-4567
Apple Sauce
My idea is to somehow split based on the 4 numbers at the end of the number sequence. I can't split on spaces. I've figured out how to split based on a single character...
words2 = s2.Split(New Char() {"|"c})
but can't seem to figure this out. Any help is much appreciated.
Dim r = New Regex("(\d+-\d+-\d\d\d\d )")
Dim m As String() = r.Split(s2).Skip(1).ToArray()
Make sure to Import System.Text.RegularExpressions
s2 Is the imput string and m is the output array
I want two strings in the end. One being the number, one being whats
left
So you want to split on the first space only? Then String.Split has an overload to take only 2:
Dim tokens = s.Split({" "c}, 2)
Here's a sample and the result:
Dim s As String = "1-23-4567 Apple Sauce and other stuff"
Dim tokens = s.Split({" "c}, 2)
Result:
1-23-4567
Apple Sauce and other stuff
Another easy and efficient approach is to use pure string methods like IndexOf and Substring:
Dim spaceIndex = s.IndexOf(" "c)
If spaceIndex > 0 Then
Dim s1 = s.Remove(spaceIndex)
Dim s2 = s.Substring(spaceIndex).Trim()
End If
try this:
Dim parts As New List(Of String)("057359-001 Pip Drt329 Auburndale, Fl (aub) - Pompano Beach, Fl (pob) 1:0 10gbe Lan Phy.".Split(" "c))
Dim myArr(1) As String
myArr(0) = parts(0)
parts.RemoveAt(0)
myArr(1) = String.Join(" ", parts.ToArray)
Try this .
var target = "1-23-4567 Apple Sauce";
var regEx = new Regex(#"\d\d\d\d"); //you are splitting on first 4 digit together
var splitStrings = regEx.Split(target).ToArray();
var stringItem = splitStrings.Last().ToString(); //This is "Apple Sauce"
var digits = s.Substring(0, (s.Length - stringItem.Length)); //This is 1-23-4567
I want to delete all the words in a textbox from starting to the first comma appearance.
Example -
Ram bought rice, bread and tomatoes.
The above sentence should become -
bread and tomatoes.
I know how to replace texts in VB.Net but I can't find a solution for this issue.
Try this:
Dim s As String = "Ram bought rice, bread and tomatoes."
Dim index As Integer = s.IndexOf(",") + 1
Dim substring As String = s.Substring(index , s.Length - index)
This is an example how to do it.
Module Example
Sub Main()
Dim str As String = "foo, bar baz"
Dim index As Integer = str.IndexOf(",") + 1
Dim length As Integer = str.Length - index
str = str.Substring(index, length)
Console.WriteLine(str)
Console.ReadLine()
End Sub
End Module
I know I'm a little late to the party, but here is how I would do it using a function.
Private Function Foo(ByVal str As String) As String
If str.Contains(",") Then
Return str.Split(",")(1).Trim()
End If
''return nothing as the user only wants the 2nd half of the string
Return Nothing
End Function
Trim() will remove any leading spaces.
You can call the function like so,
Dim value As String = "Ram bought rice, bread and tomatoes."
textBox1.Text = Foo(value)
Output: bread and tomatoes.
The other answers are nice, but adding the 2nd parameter gives an un-needed risk. Try this for safer code
Dim MyString As String = "Ram bought rice, bread and tomatoes."
Dim index As Integer = MyString.IndexOf(",") + 1
Dim MyNewString As String = MyString.Substring(index)
This just uses the first parameter, as the second calculation to calculate the length is not needed.
Say I have a string that = Grilled Cheese
How would I echo the string without the space between Grilled Cheese with out changing the string?
Also if I had a string that variables but will always have a space, like a full name. I can show the first letter of the string:
WScript.Echo Left(strUserName, 1) will echo for example G
but how could I show the name after the space (= Cheese). Keep in mind the name after the space may change like "Cheesy" for example
WScript.Echo Mid(strUserName,null) does not work
Thanks!
The Replace function can be used to remove characters from a string. If you just echo the return value without assigning it back to the variable, the original string will remain unchanged:
>>> str = "Grilled Cheese"
>>> WScript.Echo Replace(str, " ", "")
GrilledCheese
>>> WScript.Echo str
Grilled Cheese
Another option would be the Split and Join functions:
>>> str = "Grilled Cheese"
>>> WScript.Echo Join(Split(str, " "), "")
GrilledCheese
>>> WScript.Echo str
Grilled Cheese
Split will also allow you to echo just particular tokens from the original string:
>>> str = "Grilled Cheese"
>>> WScript.Echo Split(str, " ")(1)
Cheese