本文介绍了检测文本字段中的位置和变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查我的 textfieldDidChange 方法中字符串的更改内容和位置?

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?"

假设我的用户决定编辑文本和链接 markus 而不是 marie

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?"

我需要一种方法来检测文本字段中@Markus 的变化,但不会改变字符串中的任何其他内容.我能够检测到最后一个词,第一个词等等,但重要的是还要查看文本中的变化

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

我正在考虑一种方法

a = "Hello how are you"
b = newly changed text field
c = ", nice day uh?"

let str3 = a + b + c // More or less

也许我得到了我正在编辑的索引,在这个索引中取出单词——从左边的空格到右边的空格——然后把它剪掉?提前致谢,是的,我是初学者:P

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

推荐答案

您可以使用此代码拆分字符串.

You can split your string using this code.

对于目标 c :

1.创建这样的函数.

 -(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.像这样使用这个功能.

[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.

这篇关于检测文本字段中的位置和变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:20
查看更多