Validate string contains specific word - swift

I have to validate last name for example if my last name it's a compound and contains "DE", "DEL", "DE LA" what is the best way to identify that and eliminate
For example if my last name is "DEL ROSAL", I want to identify "DEL" and then remove but if my last name it is "DELGADO" what happens with "DEL"
I read that I can use contains if it is possible?
let apellido_paterno = TxtApellidoP.text!
if apellido_paterno.contains("DEL"){
// Expected output if my last name its "DEL ROSAL": ROSAL
// What happens if my last name its: "DELGADO" ???
}

I managed to get the result you wanted by using .contains() and evaluating a String assigned as "compounder".
For example if the compound last name is "DE LA FUENTE", the compounder would be "DE LA" (as you explained above).
I also created an array and a for loop in order to make it easier to check
let apellidoPaternoInput = "DE LA FUENTE"
var apellidoPaternoOutput = String()
let compounderArray = ["DEL", "DE LA", "DE"]
for compounder in compounderArray {
if apellidoPaternoInput.contains(compounder){
apellidoPaternoOutput = apellidoPaternoInput.components(separatedBy: " ").last ?? ""
//last component of an array containing all the words from the input last name
}
}
print(apellidoPaternoOutput)
Note that a new variable, apellidoPaternoOutput is created, and it corresponds to the last element of an array generated when the input last name (apellidoPaternoInput) is separated by spaces.
By doing this (using the last element of the array separated by spaces) you make sure that last names such as "DELGADO" remain intact.
In this case, the output would be "FUENTE".
Hope this helps.

Related

Flutter remove whitespace, commas, and brackets from List when going to display as a String

I want to go through my list of strings, and add it to a text element for display, but I want to remove the commas and remove the [] as well as the whitespace, but leave the symbols except the commas and brackets.
So if the List is.
[1,2,#3,*4,+5]
In the text field I want it to show - "12#3*4+5"
I can figure out how to display it, but Im using
Text(myList.tostring().replaceAll('[\\]\\,\\', '')
Is there a way to do this?
You should use the reduce method on your list.
List<String> myList = ["1", "2", "#3", "*4", "+5"];
String finalStr = myList.reduce((value, element) {
return value + element;
});
print(finalStr);
# output: "12#3*4+5"
This method reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
The method takes a function that receives two parameters: one is the current concatenated value, which starts out with the value of the first element of your list, and the second parameter is the next element on your list. So you can do something with those two values, and return it for the next iterations. At last, a single reduced value is returned. In this case, using strings, the code in my answer will concatenate the values. If those were numbers, the result would be a sum of the elements.
If you want to add anything in between elements, simply use the return value. For instance, to separate the elements by comma and whitespace, it should look like return value + " ," + element;.
Unless I'm misunderstanding the question, the most obvious solution would be to use List.join().
List<String> myList = ["1", "2", "#3", "*4", "+5"];
print( myList.join() );
// Result
// 12#3*4+5
You could also specify a separator
print( myList.join(' ') );
// Result
// 1 2 #3 *4 +5

How to get upper str this case

I want to get upper"T"..
how to get upper string!
str = "Test Version"
print(str.upper())
print(str[3])
It's not clear what you are asking.
But from context I am guessing you would like to make the second non-capitalised "t" in the string uppercase. I'm also going to assume you are using python 3 given your use of upper().
If you just want to get the "t" (and not change the string itself):
upper_T = str[3].upper()
If you want to create a string from the original you may be running into the fact that strings in python are immutable. You therefore must create a new string.
One way do this:
str2 = list(str)
str2[3] = str[3].upper()
str2 = ''.join(str2)

Index and length must refer to a location within the string.?

My input strings are
inputData = "99998UNKNOWN"
inputData = "01000AMEBACIDE/TRICHOM/ANTIBAC 1"
inputData = "34343AMEBACIDE/TRICHOM/ANTIBACSADWA1"
ID = inputData.Substring(0,5);
Name = inputData.Substring(5,30);
Level = inputData.Substring(35,1);
I am getting the below error,
Index and length must refer to a location within the string.
I can understand , the error is due to the length that specified in substring for "Name" is not matching with first input.
Is there any way to handle this issue with any input length?
One approach is to add a "sentinel" suffix to the end of the string before taking substrings. Now you can add it to the data string before taking substrings from it. As long as the suffix has sufficient length, you would never get an index/length exception:
var padded = inputData.PadRight(32);
ID = padded.Substring(0, 5).Trim();
Name = padded.Substring(5, 30).Trim();
Level = padded.Substring(30, 1).Trim();
However, now your code should check if ID, Name, or Level is empty.

Autohotkey: Splitting a concatenated string into string and number

I am using an input box to request a string from the user that has the form "sometext5". I would like to separate this via regexp into a variable for the string component and a variable for the number. The number then shall be used in a loop.
The following just returns "0", even when I enter a string in the form "itemize5"
!n::
InputBox, UserEnv, Environment, Please enter an environment!, , 240, 120
If ErrorLevel
return
Else
FoundPos := RegExMatch(%UserEnv%, "\d+$")
MsgBox %FoundPos%
retur
n
FoundPos, as its name implies, contains the position of the leftmost occurrence of the needle. It does not contain anything you specifically want to match with your regex.
When passing variable contents to a function, don't enclose the variable names in percent signs (like %UserEnv%).
Your regex \d+$ will only match numbers at the end of the string, not the text before it.
A possible solution:
myText := "sometext55"
if( RegExMatch(myText, "(.*?)(\d+)$", splitted) ) {
msgbox, Text: %splitted1%`nNumber: %splitted2%
}
As described in the docs, splitted will be set to a pseudo-array (splitted1, splitted2 ...), with each element containing the matched subpattern of your regex (the stuff that is in between round brackets).

jquery syntax to look for a hidden field in a form

I have a form with a table in it. In each row is a table cell with a hidden input item with the name of it starting with "hf_id_" followed by a number so that row 1's field has a name of "hf_id_1", row 2 is "hf_id_2" and so on. I need to search all of these fields for a particular value but I'm not quite sure how to get to the hidden fields. I know how to get to them when the full name is known but in this case I'm not sure if there's a way to get an array of these where name starts with "hf_id_". Thanks.
You can search elements with ^ (starting with) and $ (ending with), example:
$('input[name^="hf_id_"]');
So you can get all those elements like:
var elements = $('input[name^="hf_id_"]');
And you can iterate over them to search for a particular value like:
$('input[name^="hf_id_"]').each(function(){
if ($(this).val() === 'search value here')
{
// found..........
}
});
Or you could simply use
$('input[type="hidden"]');