I am trying to get words with a certain prefix when a user types into a text box. For example, let's say I want all words in a text box that begins with "#". How would I go about getting these words?
One way to do it is to use the JavaScript split() method.
Let's say you get the value of a user input into one of your variables. You can use this method to get an array of String of the words that start with the prefix you chose.
var input = "#This is what the #user typed";
var splitArray = input.split("#");
// splitArray = ["This is what the", "user typed"]
Related
I have a JSON file I am grabbing from a remote server and decoding it. It is being decoded into a struct and then used in an #Published var.
For example:
JSON:
[{"keyword": "foo"}, {"keyword": "blah"}]
Struct:
struct keywords: Codable {
var keyword: String
}
Observable Class:
#Published var keys: [keywords] = []
I need to do a real-time comparison, using contains, against a value the user is entering, utilizing an if statement. I can get it to use the first entry in the keys var and check against any characters that might be in that string, but I cannot get it to work across the entire array on only the full strings (not individual characters).
Here's what correctly checks against only the first entry of the array and each individual character.
if keys[0].keyword.contains(where: blah.contains)
I have also tried mapping it to strings like this (does exactly the same thing):
if (keys[0].keyword.map{String($0)}).contains(where: blah.contains)
Been at this all day but cannot find any docs on how to do this correctly.
The goal is to not just have it use the first entry of the array but the entirety of the entries of the array. I understand that is the [0] but it wouldn't compile without it. I need to make it compare on the entire string, not on individual characters.
For example, if the word blah is the array and the user enters bla it should not match anything. Only if the entire word blah is contained in the user's entry should it match (i.e. fooblahfoo would match because blah is within the string, but foobalbabh would NOT match even though all of the characters contained within the word blah are in that string).
Appreciate any assistance with actual code examples. This is Swift 5.5.
UPDATE:
Not sure where the confusion is coming from but here's an even clear explanation.
There is a TextField called username where the user enters a string.
If the user enters the string fooblahfoo and the word blah is somewhere in the array it should match.
This is easy when you have a simple array of strings like this:
keywords = ["foo", "blah"]
For that you just do keywords.contains(where: username.contains)
However, the decoded JSON is more of a dictionary than a simple array of strings, so you have to call to the key of keyword and look at all the values in order to make the comparison.
First, use contains(where:) on the keys array -- then, check each item to see if the user input contains that keyword.
struct Keywords: Codable {
var keyword: String
}
var keys: [Keywords] = [.init(keyword: "foo"),.init(keyword: "blah")]
var userInput = "fooblahfoo"
var result = keys.contains { userInput.contains($0.keyword) }
print(result)
true
I am using a pod called iOSDropDown to display a dropdown selection menu for a textfield. I am getting a list of data from php to populate that selection menu that Im storing in a variable.
The PHP data passed stored in a swift variable looks like this when printed -> "option01","option02","option03"... and so on. This is dynamic data that will change that is why I am retrieving from PHP/MYSQL Database instead of just manually typing in the options in the Swift array.
Below is my code. What I am trying to do is use the "dropdownData" variable that holds the options for the array. Each option should be in its own row and separately selectable. What I am getting is one option, one string of coding with all my options as shown in the picture below.How would I use the dropdownData variable to display options instead of one string, one option?
dropdownData = "option01","option02","option03"... etc. ALL OPTIONS STORED IN THIS ONE ARRAY
let dropdownData : String = (dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") as! String
.
cell.nameField.optionArray = [dropdownData]
Image
In the image above there should be no comma after styrofoam cooler... the next product should be the next option displaying under the styrofoam cooler and separately selectable.
Seems like dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") returns a String where names are comma separated,
so
if let nameString = dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") as? String {
let namesArray = nameString.components(separatedBy: ",")
cell.nameField.optionArray = namesArray
}
That should do
On Selenium, I'm writing script to get the number from the text. suppose there is a field 'Status(2)'. The number in the brackets keep changing. I want to get the value.
This code should get back the text for the element you have provided:
WebElement web_element_found = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects"));
String element_text = web_element_found.getText();
Then you can have a look at this answer for how to use regex to extract the digit from the string: Regex to extract digit from string in Java
Hope this helps!
Here is the solution.
String rawText = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects")).getText();
String number = rawText.substring(s.indexOf("(") + 1).substring(0, s.indexOf(")"));
System.out.println(number);
Problem:
Hello, I have been struggling recently in my programming endeavours. I have managed to receive the output below from Google Speech to Text, but I cannot figure out how draw data from this block.
Excerpt 1:
[VoiceMain]: Successfully initialized
{"result":[]}
{"result":[{"alternative":[{"transcript":"hello","confidence":0.46152416},{"transcript":"how low"},{"transcript":"how lo"},{"transcript":"how long"},{"transcript":"Polo"}],"final":true}],"result_index":0}
[VoiceMain]: Successfully initialized
{"result":[]}
{"result":[{"alternative":[{"transcript":"hello"},{"transcript":"how long"},{"transcript":"how low"},{"transcript":"howlong"}],"final":true}],"result_index":0}
Objective:
My goal is to extract the string "hello" (without the quotation marks) from the first transcript of each block and set it equal to a variable. The problem arises when I do not know what the phrase will be. Instead of "hello", the phrase may be a string of any length. Even if it is a different string, I would still like to set it to the same variable to which the phrase "hello" would have been set to.
Furthermore, I would like to extract the number after the word "confidence". In this case, it is 0.46152416. Data type does not matter for the confidence variable. The confidence variable appears to be more difficult to extract from the blocks because it may or may not be present. If it is not present, it must be ignored. If it is present however, it must be detected and stored as a variable.
Also please note that this text block is stored within a file named "CurlOutput.txt".
All help or advice related to solving this problem is greatly appreciated.
You could do this with regex, but then I am assuming you will want to use this as a dict later in your code. So here is a python approach to building this result as a dictionary.
import json
with open('CurlOutput.txt') as f:
lines = f.read().splitlines()
flag = '{"result":[]} '
for line in lines: # Loop through each lin in file
if flag in line: # check if this is a line with data on it
results = json.loads(line.replace(flag, ''))['result'] # Load data as a dict
# If you just want to change first index of alternative
# results[0]['alternative'][0]['transcript'] = 'myNewString'
# If you want to check all alternative for confidence and transcript
for result in results[0]['alternative']: # Loop over each alternative
transcript = result['transcript']
confidence = None
if 'confidence' in result:
confidence = result['confidence']
# now do whatever you want with confidence and transcript.
I have a requirement where the user can enter/copy-paste 25 ids separated by comma in a textarea ; there is no maximum size for the ids.Thereafter he cannot enter any characters.He then submits those ids for search.
Is there any way to limit the id number to 25 and then not allowing the user to enter anything.
I cannot use maxlength here.Also all these checking have to happen before user submits and the control reaches the angularjs controller.
I am using angularjs,HTML5,CSS3,bootstrap.
Basically what you'd need to is count the number of commas in the string and raise an error when it exceeds 24. You could do this using ng-onchange or when the user submits. This answer: Count the number of occurrences of a character in a string in Javascript should help you with counting the commas.
You can split the model of the input on ng-change like this for example ( where textareaValue is the model of the textarea ):
var textareaValueArray = textareaValue.split(',');
if( textareaValueArray.length > 25 )
textareaValue = textareaValueArray.slice(0,25).join(',')