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

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.

Related

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

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)

Manipulation with strings: Capitalize every word after any delimiter in Python

I am trying to manipulate string so it capitalizes each word after any delimiter.
Currently, I am using capwords() method imported from string module. Code sample:
my_string = "hello MY-naMe-is john"
new_string = string.capwords(my_string)
print(new_string)
Using only capwords() method, result is this:
Hello My-name-is John
Result that I am trying to get:
Hello My-Name-Is John
Is it possible to use more than one separator in capwords()? Is there a solution to this while still using capwords() method? Thanks!
Use title() in built method
my_string = "hello MY-naMe-is john"
new_string = my_string.title()
print(new_string)
#'Hello My-Name-Is John'
There is no way to use capwords and get the result that you need. The capwords(s, sep=None) documentation shows the steps that it undergoes:
str.split() - here the sep is used (if provided)
str.capitalize() - first character of each item in the list (from split) is capitalized
str.join() - the string is joined back
split takes just one sep. However, re.split takes multiple delimeters/separators.
Use title() if you want to achieve your desired result, as suggested by #Ananth.P

How to replace value of a string which is on right side after a space

I am wondering is there any way to replace this type of value in string
https://farm5.staticflickr.com/4796/39790122335_bdc207b259_o.jpg https://farm5.staticflickr.com/4776/39790122225_c8e96339fa.jpg
What i want is that replace the right side of URL and just show the left side there is little space between them.
You can do that in many different ways, using many different languages.
For example, this is how you can do it with JavaScript
var str = 'https://farm5.staticflickr.com/4796/39790122335_bdc207b259_o.jpg https://farm5.staticflickr.com/4776/39790122225_c8e96339fa.jpg'
str = str.split(' ')
str = str[0]
it can be easily done using the split function.
First assign the url to a Variable
Python
theurl = 'https://farm5.staticflickr.com/4796/39790122335_bdc207b259_o.jpg https://farm5.staticflickr.com/4776/39790122225_c8e96339fa.jpg'
each_url = theurl.split()
now your new variable each url is a list of objects containing all the URLS
i.e each_url = ['https://farm5.staticflickr.com/4796/39790122335_bdc207b259_o.jpg ', 'https://farm5.staticflickr.com/4776/39790122225_c8e96339fa.jpg']
the Split function works in pretty different Programming Languages
though in some like Javascript you might have to specify split as split(' ')
showing you are splitting by spaces

Extract numbers from String Array in Scala

I'm new to Scala and unsure of how to achieve the following
I have the String
val output = "6055039\n3000457596\n3000456748\n180013\n"
I want to extract the numbers separated by \n and store them in an Array
output.split("\n").map(_.toInt)
Or only
output.split("\n")
if you want to keep the numbers in String format. Note that .toInt throws. You might want to wrap it accordingly.

string parts seperated by ; to ASCII written in a new string

Something like that is coming in:
str="Hello;this;is;a;text"
What I do want as result is this:
result="72:101:108:108:111;116:104:105:115;..."
which should be the Text in ASCII.
You could use string matching to get each word separated by ; and then convert, concat:
local str = "Hello;this;is;a;text"
for word in str:gmatch("[^;]+") do
ascii = table.pack(word:byte(1, -1))
local converted = table.concat(ascii, ":")
print(converted)
end
The output of the above code is:
72:101:108:108:111
116:104:105:115
105:115
97
116:101:120:116
I'll leave the rest of work to you. Hint: use table.concat.
Here is another approach, which exploits that fact that gsub accepts a table where it reads replacements:
T={}
for c=0,255 do
T[string.char(c)]=c..":"
end
T[";"]=";"
str="Hello;this;is;a;text"
result=str:gsub(".",T):gsub(":;",";")
print(result)
Another possibility:
function convert(s)
return (s:gsub('.',function (s)
if s == ';' then return s end
return s:byte()..':'
end)
:gsub(':;',';')
:gsub(':$',''))
end
print(convert 'Hello;this;is;a;text')
Finding certain character or string (such as ";") can be done by using string.find - https://www.lua.org/pil/20.1.html
Converting character to its ASCII code can be done by string.byte - https://www.lua.org/pil/20.html
What you need to do is build a new string using two functions mentioned above. If you need more string-based functions please visit official Lua site: https://www.lua.org/pil/contents.html
Okay...I got way further, but I can't find how to return a string made up of two seperate strings like
str=str1&" "&str2

Resources