How do I take a string and turn it into a list using SCALA? - string

I am brand new to Scala and having a tough time figuring this out.
I have a string like this:
a = "The dog crossed the street"
I want to create a list that looks like below:
a = List("The","dog","crossed","the","street")
I tried doing this using .split(" ") and then returning that, but it seems to do nothing and returns the same string. Could anyone help me out here?

It's safer to split() on one-or-more whitespace characters, just in case there are any tabs or adjacent spaces in the mix.
split() returns an Array so if you want a List you'll need to convert it.
"The dog\tcrossed\nthe street".split("\\s+").toList
//res0: List[String] = List(The, dog, crossed, the, street)

Related

Finding substring within string

I want to find a specific string within a string.
For example, let's say I have the string
string = "username:quantopia;password:blabla
How can I then find quantopia?
I am using python 3.
Update: I am sorry I did not mention what I try before..
string.split('username:',1)[1].split(';',1)[0]
But this look very bad and not efficient, I was hoping for something better.
Just use regex as such:
import re
username = re.search("username:(.*);password", "username:quantopia;password:blabla").group(1)
print("username:", username)
This will output quantopia.
In this expression "username:(.*);password" you are saying "give me everything from username: to ;password" So this is why you're getting quantopia. This might as well be ":(.*);" as it will output the same thing in this case.
The simple solution is:
string = "username:quantopia;password:blabla"
username = "username"
if username in string:
# do work.
You might be better to just use split to create a dictionary so you dont need to use multiple regex to extract different parts of data sets. The below will split stirng into key value pairs then split key value pairs then pass the list of lists to dict to create a dictionary.
string = "username:quantopia;password:blabla"
data = dict([pairs.split(':') for pairs in string.split(';')])
print(f'username is "{data["username"]}" and password is "{data["password"]}"')
OUTPUT
username is "quantopia" and password is "blabla"

Find index of a specific character in a string then parse the string

I have strings which looks like this [NAME LASTNAME/NAME.LAST#emailaddress/123456678]. What I want to do is parse strings which have the same format as shown above so I only get NAME LASTNAME. My psuedo idea is find the index of the first instance of /, then strip from index 1 to that index of / we found. I want this as a VBScript.
Your way should work. You can also Split() your string on / and just grab the first element of the resulting array:
Const SOME_STRING = "John Doe/John.Doe#example.com/12345678"
WScript.Echo Split(SOME_STRING, "/")(0)
Output:
John Doe
Edit, with respect to comments.
If your string contains the [, you can still Split(). Just use Mid() to grab the first element starting at character position 2:
Const SOME_STRING = "[John Doe/John.Doe#example.com/12345678]"
WScript.Echo Mid(Split(SOME_STRING, "/")(0), 2)
Your idea is good here, you should also need to grab index for "[".This will make script robust and flexible here.Below code will always return strings placed between first occurrence of "[" and "/".
var = "[John Doe/John.Doe#example.com/12345678]"
WScript.Echo Mid(var, (InStr(var,"[")+1),InStr(var,"/")-InStr(var,"[")-1)

How to print specific number of words from a string in scala?

I have a strings:
str = "this is a great place...."
I want to print only 30 words from this string. How to do that?
Use split and take methods:
val str = "this is a great place...."
str.split("\\W").take(30).mkString(" ")
// res0: String = this is a great place
You could just do something like:
"""(\b\w+\b\W*){0,30}""".r findPrefixOf "this is a great place...."
Or using a different notation:
"""(\b\w+\b\W*){0,30}""".r.findPrefixOf("this is a great place....")
Here is some pseudo code you can work with
Split string using the split method into an Array[String] of the words.
Iterate across the array and concatenate the words together that you want to include
Print out the string
I can't think of any external libraries or built-in functions that will do that for you. You will need to write your own code to do this.

Reading from a string using sscanf in Matlab

I'm trying to read a string in a specific format
RealSociedad
this is one example of string and what I want to extract is the name of the team.
I've tried something like this,
houseteam = sscanf(str, '%s');
but it does not work, why?
You can use regexprep like you did in your post above to do this for you. Even though your post says to use sscanf and from the comments in your post, you'd like to see this done using regexprep. You would have to do this using two nested regexprep calls, and you can retrieve the team name (i.e. RealSociedad) like so, given that str is in the format that you have provided:
str = 'RealSociedad';
houseteam = regexprep(regexprep(str, '^<a(.*)">', ''), '</a>$', '')
This looks very intimidating, but let's break this up. First, look at this statement:
regexprep(str, '^<a(.*)">', '')
How regexprep works is you specify the string you want to analyze, the pattern you are searching for, then what you want to replace this pattern with. The pattern we are looking for is:
^<a(.*)">
This says you are looking for patterns where the beginning of the string starts with a a<. After this, the (.*)"> is performing a greedy evaluation. This is saying that we want to find the longest sequence of characters until we reach the characters of ">. As such, what the regular expression will match is the following string:
<ahref="/teams/spain/real-sociedad-de-futbol/2028/">
We then replace this with a blank string. As such, the output of the first regexprep call will be this:
RealSociedad</a>
We want to get rid of the </a> string, and so we would make another regexprep call where we look for the </a> at the end of the string, then replace this with the blank string yet again. The pattern you are looking for is thus:
</a>$
The dollar sign ($) symbolizes that this pattern should appear at the end of the string. If we find such a pattern, we will replace it with the blank string. Therefore, what we get in the end is:
RealSociedad
Found a solution. So, %s stops when it finds a space.
str = regexprep(str, '<', ' <');
str = regexprep(str, '>', '> ');
houseteam = sscanf(str, '%*s %s %*s');
This will create a space between my desired string.

how to split a string or make chars in vb 2010

I searched but nothing explains how to do this,
for example
Dim sentence as String = "cat is an animal"
if i make a msgbox :
MsgBox(sentence)
it shows
cat is an animal
how to make a msgbox that says
cat
is
an
animal.
Easy way Replace space with new line
as in string words = MyString.Replace(" ","\r\n")
Split would be split on space in to an array , and then join that back up with new lines which is pointless unless you need the array for something else.

Resources