Change substring color within dynamic string [duplicate] - ios

This question already has answers here:
UITextView change text color of specific text
(3 answers)
Closed 7 years ago.
I have an iOS Swift 2/Xcode 7 project that searches a string and identifies substrings from a specified list. The input string will change, but the substrings remain the same.
Something like this
inputString "SALT, WATER, FLOUR"
Comparison substrings "SALT, PEPPER"
Since "SALT" is present, it would become highlighted to look like this
SALT WATER FLOUR
and ideally in a different color - so Red for "SALT", Green for "PEPPER", anything else would be black.
Here is the basic code for searching for the substrings -
iList = "SALT, FLOUR, WATER...." //actually pulled from CORE data, different for every search
for ingredient in ["SALT", "PEPPER", "SUGAR"] {
if iList.rangeOfString(ingredient) != nil {
print("Ingredient Found \(ingredient) found")
}
}
The iList is displayed in a TextView.
The desired result would look something like
SALT, FLOUR, WATER....
The examples I found relied on being able to change text colors at specific positions within the string. In this case, the location of a substring (if present) will vary for every use.
Help with this would be greatly appreciated.
Thank you.

You need to use NSAttributedString for this purpose.
Check out this post:
http://ramezanpour.net/post/2014/01/28/customize-your-texts-in-ios-using-nsattributedstring/
Use substringWithRange method to find the range of the string that you need to make bold or change color and then use the returned range as shown in the tutorial.

Related

How can I FIND the last instance of a character in a string? (Google Sheets)

I need a Google Sheet function that will return the position of the last instance of a particular character. Basically, FIND, but starting on the right.
For example, for the data set below, I need to return the position of the last dash.
ABC-DEF-GHI = 8
ABCD-EF-GH-IJK = 11
AB-C-DE-FGH-I-JK = 14
Thanks!
I don't know where to start. MID might work, but the file names are of different lengths and different formats. The files just generally end with - ***.png, and I need the asterisk. The string I need is also of variable length and can contain spaces (the string is the name of the student).
Here's a possible solution:
=len(regexextract(A1,".*-"))
It's essentially extracting everything up to the last dash and taking the length of the resulting string.
for the whole array try:
=INDEX(LEN(REGEXEXTRACT(A1:A3; "(.*-)")))

Partial match a range of strings with another range of strings, outputting the partial match [duplicate]

This question already has an answer here:
How to index match an array of cells to see if each one will match? [duplicate]
(1 answer)
Closed 2 years ago.
I want to partial match the following range of strings:
against this range of strings:
and if there is a match, output the original values from the original range:
I've tried nested IFS and VLOOKUPS, which strangely didn't work for me:
=IF(VLOOKUP(""&$B$1&"",E1,1,FALSE)=E1,$B$1,IF(VLOOKUP(""$B$2&"",E1,1,FALSE)=E1,$B$1)
I've also tried:
=SUMPRODUCT(--ISNUMBER(SEARCH(B1:B22,E1)))
Which got close, but it produced only digits for me in the end, when I want the original string from the 1st range I'm using.
I only want to use formulas, not script.
Using your SUMPRODUCT construction and adding "*" before and after the search string and using it inside an index function:
=IFERROR(INDEX($B$1:$B$22,IF(--ISNUMBER(SEARCH("*"&$B$1:$B$22&"*",$E$1:$E$25))=1,ROW($B$1:$B$22),"")),"")
You can use below formula and see if it helps your situation.
=LOOKUP(2^15,SEARCH($B$1:$B$22,E1,1),$B$1:$B$22)
Here 2^15 is a number larger than maximum number of characters that Excel cell can hold.
Note: I am sure about this formula in Excel. However, I have not tested this formula in google-sheets.

How to remove Text in the center of a Cell

So I am going around this the long way but what I am trying to do is extract the text in the center of a cell with text on both sides of it.
Example Text is:
Alliance: CRAZY CATS (Neutral)
Alliance: Dark Arts (Yours)
Alliance: Portal (Hostile)
I want to extract everything between : and (
In these cases I only need the name of the alliance the person is in. I tried to do a regedit but didn't completely understand it. I tried a few different formulas but was only able to remove either the first part or the last part and I could push them to two different cells but couldn't get them to work together in the same cell.
You can use REGEX functions, but I find that not everyone understands them. So here, I'll supply a non-REGEX solution.
Supposing your sample data were in A2:A4 (with some header in A1), place the following in B2 (or row 2 of any other column):
=ArrayFormula(IF(A2:A="","",TRIM(MID(A2:A,FIND(":",A2:A)+1,FIND("(",A2:A)-FIND(":",A2:A)-1))))
=REGEXEXTRACT(A2, ":\s*([^(]+)\s*\(")
: literal ":"
\s* zero or more space characters
() capture group
[^(]+ one or more of any character except (
\( literal (
See
What does this regex mean?
Tag info page

detect where and what changes within textfield

How can I check what exactly and where a string changed in my textfieldDidChange method?
let str1 = "Hello how are you #Marie, nice day uh?"
So imagine my user decides to edit the text and link markus instead of marie
let str2 = "Hello how are you # Markus, nice day uh?"
I need a method that detect the change of # Markus in the text field but doesn't change anything else in the string.
I am able to detect last word, first word and so one, but its important to also see what changed within the text
i am thinking about a method that
a = "Hello how are you"
b = newly changed text field
c = ", nice day uh?"
let str3 = a + b + c // More or less
Maybe that I get the index where I am editing at, taking the word out at this index - from left space to right space - and cutting it out?
Thanks in advance, yes I am a beginner :P
If you set your view controller up to be the delegate of your text field, you can implement the textField(_:shouldChangeCharactersIn:replacementString:) delegate method. That will tell you the range of characters that the user has asked to change, as well as providing the replacement string.
You could use that to monitor the user's edits. However, the string can easily get changed in a way that is difficult to track. What if the user deletes the "Hello how are you " part? What if they replace the whole string with "Shut your festering gob #fred, you malodorous pervert"?
You're probably better off getting the whole string from the text field and parsing it. You could accept anything the user enters, an look for and # sign, and take everything after that until the next space as the name, e.g. #alphanumerics`
Edit:
Alternately, you could set up your screen so the first part is a label, the middle part is an editable text field, and the last part is another label. The only part the user is able to edit is the middle part, the name. Or you could make it 3 separate text fields, the prefix, the name, and the suffix. Tell the user what you expect them to enter into each part.
You can split your string using this code.
For Objectve c :
1. Create function like this.
-(NSString *)checkString:(NSString *)splitString{
NSRange firstObj = [splitString rangeOfString:#"#"];
NSRange secondObject = [splitString rangeOfString:#","];
NSRange rangeSubStr = NSMakeRange(firstObj.location + firstObj.length, secondObject.location - firstObj.location - firstObj.length);
return [splitString substringWithRange:rangeSubStr];
}
2. Use this function like this.
[self checkString:#"Hello how are you #Marie, nice day uh?"]
Then every change in text, you can check substrings, and compare them to original substrings.

I have text in B1:B15, if text in B17 = anything from B1:B15 return in C17 true otherwise false

I'm trying to write a string that can cover off all text in B1:B15 but get stuck when comes to adding beyond the first part which I can get to work.
I have this so far =IF((B17=B1),"North")
I then want to continue so covers off B1:B8 as North and B9:B15 as South based on selection made
Example
Your question title says you want to see 'True' or 'False' in C17 depending on whether or not the value in B17 is in the list B1:B15. Try this in C17:
=NOT(ISNA(VLOOKUP(B17,B1:B15,1,FALSE)))
If instead of just getting true/false you want to return the value in column C depending on which value was matched in column B then you would use:
=VLOOKUP(B17,B1:C15,2,FALSE)
I don't quite understand what you're asking because your example image has nothing in B17.I assume you're writing your equation in C1?
If I'm following what you're asking, what you actually probably want to do is, in C1:
=IFERROR(VLOOKUP(B17,$B$1:$C$15,2,0),"Not found")
Then fill that equation down as far as you need to go in column C. The iferror means if the vlookup fails to find something, it'll display that message, which you can make whatever.
I will follow your logic
=IF(COUNTIF(B1:B16,A17)>0,"True","false")
"If the number of coincidences of A17 in the range B1:B16 is bigger than 1 then 'True', else 'false'"

Resources