Extracting Specific Strings From Text - string

I am trying to extract specific strings from my text. I have a string similar to this:
"blablabla(/)Hello Bob(|)bla(/)Hi(|)blablaba"
and I am trying to concatenate a string array of text between (/) and (|) but I cannot figure out a efficient way to do this. In this example I would want to return "Hello Bob" and "Hi". How can I effectively extract specific strings from a string in Swift?

Using a functional approach, you can:
split the string by (/), which returns an array of strings
for each element of the array, transform it into an array of strings split by (|)
filter the resulting array including only values (arrays) having more than one element
transform each element (array) of the resulting array into its first element
This is the code:
let array = string.componentsSeparatedByString("(/)")
.map { $0.componentsSeparatedByString("(|)") }
.filter { $0.count > 1 }
.map { $0[0] }
Given this input string:
let string = "blablabla(/)Hello Bob(|)bla(/)Hi(|)blablaba"
the result is:
["Hello Bob", "Hi"]

Related

Prolog concatenate all elements inside a list to make a string?

How would I concatenate all the elements in a list together in prolog? I am trying to integrate it with my current function that outputs a list.
input = [a,b,c,d]
output = "abcd"
If you input list contains characters then use this: Here list contains Characters and in X you get result back.
concate(X,List):-
atom_chars(X,List).
For this query you get result as this:
concate(X,[a,b,c,d]).
X = abcd.
Here X gets result as atom not string.
Now if you want a string back and you list contains atoms then you use this:
atomsToString(List,X):-
atomics_to_string(List,X).
With this query:
?- atomsToString([a,b,c,d],X).
X = "abcd".
Here X contains a string.

How to Split a string with a set of delimiters and find what delimiter it was? Kotlin

So I am learning Kotlin now, and I was trying to do a calculator where if we can give expression like 4+3 or 3*5 and we will get the answer so I was trying to split that input string and then find what operator is used and what are the operands.
var list = str.split("+","-","*","/" )
so how can i get the delimiter that is used to split that string too.
I'm afraid that split method doesn't have this feature. You would have to split the the string via separate split calls. And compare the outcome with original string. If the string wasn't split by given delimiter that outcome should be the same.
Eg. like this:
var str = "5+1"
var delimiters = arrayOf("+","-","*","/")
var found = "Not found"
for (delimiter in delimiters) {
var splited = str.split(delimiter)
if(splited[0] != str) {
found = delimiter
break
}
}
println(found)

How to split string using scala?

I have a following string and I want to split it using scala
"myInfo": "myName-name;model-R210;"
I want to split value of myInfo string such that I can access myName and its value seperately.
e.g. myName:name, model:R210 etc
I am using following code to split string -
(mainString \ "myInfo").as[String].split("\\;").toList.map(_.split("\\-"))
where mainString is Json and contains 'myInfo' key value pair.
How do I split string to seperate it by '-' and access it?
You can obtain a Map[String,String] like so:
val data: Map[String,String] = (mainString \ "myInfo").as[String]
.split("\\;").map(_.split("\\-").toList)
.collect {
case key :: value :: _ => key -> value
}.toMap
Then access your values:
val name = data.getOrElse("myName", "DefaultNameIfMissing")
First of all, shame on whoever encoded a complex data structure into a string in a JSON document; you shouldn't have to parse it at all. If it's under your control, I'd change that to something like
"myInfo": {
"myName": "name",
"model": "R210"
}
But if you can't change the input, then just do this to get the Map you want:
val myInfo = ((mainString \ "myInfo").as[String] split ';' map (_ split '-') collect { case Array(k,v) => k->v } ).toMap
No need to create Lists out of the intermediate results -- that would just slow things down. And just split on a Char, not a String (which would get compiled as a regular expression).
Note that the collect causes any component with no hyphen or more than one hyphen to be ignored; you might want to do something else there.

Best way to compare multiple string in java

Suppose I have a string "That question is on the minds of every one.".
I want to compare each word in string with a set of word I.e. (to , is ,on , of) and if those word occurs I want to append some string on the existing string.
Eg.
to = append "Hi";
Is = append "Hello";
And so on.
To be more specific I have used StringTokenizer to get the each word and compared thru if else statement. However we can use Switch also but it is available in Jdk 1.
7.
I don't know if this is what you mean, but:
You could use String.split() to separate the words from your string like
String[] words = myString.split(" ");
and then, for each word, compare it with the given set
for(String s : words)
{
switch(s)
{
case("to"):
[...]
}
}
Or you could just use the String.contains() method without even splitting your string, but I don't know if that's what you wanted.
Use a HashMap<String,String> variable to store your set of words and the replacement words you want. Then split your string with split(), loop through the resulting String[] and for each String in the String[], check whether the HashMap containsKey() that String. Build your output/resulting String in the loop - if the word is contained in the HashMap, replace it with the value of the corresponding key in the HashMap, otherwise use the String you are currently on from the String[].

Understanding the string represented data type in Matlab

I have a String like '12,23,43,erogol,bla,3.4' and I want to parse it and see which type of values are in this string. For example if I give that string to function I expect to have a vector like output=["integer,integer,integer,string,string,double"] as the return of the function.
How could I do it in matlab ?
This can be done very easily using regular expressions.
input = '-12,12,0,erogol,bla,3.4,-3.4';
First replace the strings with 'string'.
Next catch the doubles
Finally catch the integers:
Example:
output = regexprep(input, '[a-zA-Z]*', 'string');
output = regexprep(output, '[-]*[0-9]*[.][0-9]*', 'double');
output = regexprep(output, '[-]*[0-9]*', 'integer');
Output now contains `integer,integer,integer,string,string,double,double'
Which you can split into a cell array using:
varTypes = regexp(output, ',', 'split');

Resources