Source string concatenation and extraction methods, apparatus, electronic devices and storage media
Through the use of predefined key-value pairs of rules and characteristic string objects, the problem of large memory consumption and large performance loss during source string splicing and extraction is solved, and efficient and accurate source string processing is achieved, which is suitable for iOS development string stitching operation.
Patent Information
- Application Number
- CN202310317756.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-28
- Publication Date
- 2024-11-08
- Estimated Expiration
- 2043-03-28
AI Technical Summary
The prior art has problems such as large memory consumption, large performance loss, high operational complexity and inability to meet business needs during the splicing and extraction of source strings.
The corresponding key-value pair is set for each source string through the predefined key-value pair rules, stored as a characteristic string object, and the splicing operation is performed using the preset splicing method. The source characters are extracted and processed later through the enumeration attribute function. string, avoiding additional variable storage and complex calculations.
Reduces memory consumption and performance loss, simplifies operations, improves the efficiency and accuracy of source string splicing and extraction without the need for source strings to meet specific rules.
Smart Images

Figure CN116306500B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a source string concatenation and extraction method, device, electronic device and storage medium. Background Art
[0002] In some business scenarios, strings from different sources (source strings) need to be concatenated to generate a result string. In subsequent processing, one or more source strings may need to be extracted from the result string for operation.
[0003] At present, the concatenation and extraction of source strings mainly include the following technical solutions: Solution 1: Store the source string in different variables and access the corresponding variables during post-processing. This method will increase memory consumption and performance loss, especially when the string content is long or the processing timing is uncertain. Solution 2: Record the position of the source string in the concatenated string, and intercept the string at the corresponding position during post-processing. This method may lead to a complex calculation process, performance waste, and difficulty in updating the source string position. Solution 3: When the source string meets specific rules (such as regular expressions), the source string is extracted through regular expression matching during post-processing. The limitation of this method is that the source string must meet specific rules, and the regular matching results may not fully meet business requirements.
[0004] It can be seen that the existing concatenation and extraction solutions for source strings still have problems such as high memory consumption, high performance loss, high operation complexity and inability to meet business needs. Summary of the invention
[0005] In view of this, the embodiments of the present application provide a source string concatenation and extraction method, device, electronic device and storage medium to solve the problems of large memory consumption, large performance loss, high operation complexity and inability to meet business needs in the prior art.
[0006] According to a first aspect of an embodiment of the present application, a source string splicing and extraction method is provided, comprising: setting a corresponding key-value pair for each source string according to a predefined key-value pair rule, wherein the key-value pair comprises a characteristic key and a characteristic value corresponding to the source string; storing the source string as a characteristic string object, and marking the characteristic string object using the key-value pair of the source string; performing a splicing operation on the source string using a preset splicing method to obtain a spliced result string; and in post-processing, calling a predetermined enumeration attribute function to traverse the result string to obtain a marked key-value pair or characteristic value, using the key-value pair or characteristic value as an input to a callback function, and using the callback function to extract and process the source string corresponding to the key-value pair or characteristic value.
[0007] According to a second aspect of an embodiment of the present application, a source string splicing and extraction device is provided, comprising: a setting module, configured to set a corresponding key-value pair for each source string according to a predefined key-value pair rule, wherein the key-value pair includes a characteristic key and a characteristic value corresponding to the source string; a generation module, configured to store the source string as a characteristic string object, and to add a mark to the characteristic string object using the key-value pair of the source string; a splicing module, configured to perform a splicing operation on the source string using a preset splicing method to obtain a spliced result string; and an extraction module, configured to call a predetermined enumeration attribute function to traverse the result string during post-processing, to obtain a marked key-value pair or characteristic value, to use the key-value pair or characteristic value as an input to a callback function, and to extract and process the source string corresponding to the key-value pair or characteristic value using the callback function.
[0008] According to a third aspect of an embodiment of the present application, an electronic device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the steps of the above method are implemented when the processor executes the program.
[0009] According to a fourth aspect of an embodiment of the present application, a computer-readable storage medium is provided, which stores a computer program, and when the computer program is executed by a processor, the steps of the above method are implemented.
[0010] At least one of the above technical solutions adopted in the embodiments of the present application can achieve the following beneficial effects:
[0011] According to the predefined key-value pair rules, a corresponding key-value pair is set for each source string, wherein the key-value pair includes the characteristic key and characteristic value corresponding to the source string; the source string is stored as a characteristic string object, and the key-value pair of the source string is used to add a tag to the characteristic string object; the source string is spliced using a preset splicing method to obtain a spliced result string; in post-processing, a predetermined enumeration attribute function is called to traverse the result string to obtain the marked key-value pair or characteristic value, and the key-value pair or characteristic value is used as the input of the callback function, and the callback function is used to extract and process the source string corresponding to the key-value pair or characteristic value. The present application does not require additional variables to store strings, thereby reducing memory consumption and performance loss, and has no rule restrictions on source strings, which reduces the difficulty of operation and can improve the efficiency of source string splicing and extraction. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0013] Figure 1 It is a flow chart of the source string concatenation and extraction method provided in an embodiment of the present application;
[0014] Figure 2 It is a structural schematic diagram of a source string concatenation and extraction device provided in an embodiment of the present application;
[0015] Figure 3 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0016] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application may also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present application.
[0017] As described in the background technology, in many cases, business needs require concatenating strings from different sources (referred to as source strings) to form a result string. Later, it is necessary to extract one or several source strings from the result string for processing, which requires marking the source string for later processing.
[0018] The following is a detailed introduction to the implementation methods and existing problems of the existing concatenation and extraction solutions for source strings with specific examples, which may include the following contents:
[0019] Example 1: You need to extract a source string for processing
[0020] Source string alpha + source string beta + source string delta = result string alphabetadelta
[0021] Later, you only need to extract the source string beta for processing, such as making beta bold and black.
[0022] Example 2: Multiple source strings need to be taken out for processing
[0023] Source string alpha + source string beta + source string delta = result string alphabetadelta
[0024] Later, the source string alpha and the original string beta need to be taken out for processing, for example, both alpha and beta need to be bolded and darkened at the same time.
[0025] Existing technical solutions:
[0026] Solution 1: Store the source string in different variables. When you need to retrieve it later, access the corresponding variable and get the corresponding string for processing.
[0027] Solution 1 has the following disadvantages: (1) Additional variables are required to store strings, which increases memory consumption, especially when the string content is relatively long. (2) The life cycle of the storage variable must be consistent with the duration of "post-processing", but the timing of "post-processing" may be uncertain (such as relying on user interactions such as clicks that may occur at any time), so the life cycle of the variable cannot be determined either. Usually, it can only be set to the same length as the App's life cycle, resulting in performance loss.
[0028] Solution 2: When concatenating the source string, record its position in the final string (including the starting position and its length). When it is needed later, intercept the string at the corresponding position for processing.
[0029] Solution 2 has the following disadvantages: (1) The concatenation operation may be complex, so the algorithm for calculating its position may be complex, and may involve multiple traversals or loops, resulting in performance waste. (2) The concatenation operation may not be a one-time operation, but may be performed multiple times at different time points based on business needs. Each time a new operation is performed on the result string, the positions of all the original source strings must be updated, resulting in performance waste. (3) The source string itself may also change, and each time it changes, its position in the result string must be calculated, resulting in performance waste. (4) Recording the position also requires variables, so the disadvantages of Solution 1 also exist.
[0030] Solution 3: If the source string is a string with certain rules, such as satisfying the regular expression re gex, then when the source string is obtained later, the regular expression is used for matching to obtain all the source strings for processing.
[0031] Solution 3 has the following disadvantages: (1) The source string must meet certain rules to be obtained through regular expressions. (2) The set matched by the regular expression and the source string of the business requirement are only an intersection relationship. The business requirement does not necessarily want to extract all the source strings that meet the rules. For example, if you only want to extract the second one, relevant processing must be performed, which increases the difficulty.
[0032] In view of this, an embodiment of the present application provides a source string splicing and extraction method to solve the above problems. The present application sets a corresponding key-value pair for each source string through a predefined key-value pair rule, including a feature key and a feature value. The source string is stored as a feature string object, and an instance is generated, and a tag is added to the instance of the feature string object using a key-value pair. The source string is spliced using a preset splicing method to obtain a result string. During post-processing, a predetermined enumeration attribute function is called to traverse the result string to obtain a marked key-value pair or feature value, and a callback function is used to extract and process the corresponding source string. In addition, the present application also includes a detailed description of how to set key-value pair rules, generate a feature string object instance, add tags, perform splicing operations, and use different enumeration attribute functions to extract and process source strings.
[0033] The present application utilizes the attribute string object NSAttributedString and its variable variant NSMutableAttributedString, as well as the self-defined subclasses inherited from them that can mark / remove marks, to achieve the function of storing and retrieving source strings at any time. The source string splicing and extraction method provided by the present application effectively solves many problems existing in the prior art, such as memory consumption, performance loss and operation complexity, and improves the efficiency and accuracy of source string splicing and extraction.
[0034] The contents of the technical solution of the present application are described in detail below with reference to the accompanying drawings and specific embodiments.
[0035] Figure 1 It is a flowchart of the source string concatenation and extraction method provided in an embodiment of the present application. Figure 1 The source string concatenation extraction method can be executed by an iOS application. Figure 1 As shown, the source string concatenation and extraction method may specifically include:
[0036] S101, according to a predefined key-value pair rule, setting a corresponding key-value pair for each source character string, wherein the key-value pair includes a characteristic key and a characteristic value corresponding to the source character string;
[0037] S102, storing the source string as a characteristic string object, and adding a tag to the characteristic string object using a key-value pair of the source string;
[0038] S103, performing a concatenation operation on the source character string using a preset concatenation method to obtain a concatenated result character string;
[0039] S104, in the later processing, calling the predetermined enumeration attribute function to traverse the result string, obtaining the marked key-value pairs or characteristic values, taking the key-value pairs or characteristic values as the input of the callback function, and using the callback function to extract and process the source string corresponding to the key-value pairs or characteristic values.
[0040] First, some technical terms involved in the actual application scenarios of the embodiments of the present application are explained. In iOS development, string processing is a common task, which mainly involves two string classes: NSString and NSAttributedString; the following is a brief explanation of them:
[0041] NSString: is a common string object. NSString is a core class in the Foundation framework that is used to represent and manipulate immutable Unicode strings. It provides a series of methods to process strings, such as comparison, search, replacement, and splicing. NSString is immutable, which means that after creation, its content cannot be changed. This helps improve performance because memory does not need to be frequently allocated and released when performing certain operations.
[0042] For situations where you need to change the contents of a string, you can use NSMutableString, which is a subclass of NSString that allows you to modify the string contents after it is created. NSMutableString inherits all the methods of NSString and adds methods for modifying strings, such as inserting, deleting, appending, etc.
[0043] NSAttributedString: is an attribute string object. NSAttributedString is another class in the Foundation framework that is used to represent and manipulate strings with attributes. Attributes can be character color, font, underline, paragraph style, etc. Similar to NSString, NSAttributedString is also immutable, which means that after creation, its content and attributes cannot be changed. The purpose of NSAttributedString is to handle rich text, that is, text with styles, such as text displayed in a text editor or interface.
[0044] For situations where you need to change the content of a string with attributes, you can use NSMutableAttributedString, which is a subclass of NSAttributedString that allows you to modify the string content and attributes after creation. NSMutableAttributedString inherits all the methods of NSAttributedString and adds methods for modifying the string content and attributes.
[0045] NSAttributedString.Key is the key in the characteristic key-value pair of NSAttributedString, such as font color, paragraph spacing, font size, capitalization, and so on.
[0046] Structure: One of the four major types of iOS Swift language. It is a value type. It is similar to a class and has member properties and methods, but does not have the characteristics of inheritance. When it is passed between functions, it is copied by value rather than by reference.
[0047] Generally speaking, NSString is a pure string without style (the style in the embodiment of this application is the property), and NSAttributedString is a string with style. The style (i.e., property) includes: font color, paragraph spacing, font size, capitalization, etc. NSString is used to process ordinary immutable strings, while NSAttributedString is used to process immutable strings with style and attributes. For the needs of mutable strings, their subclasses NSMutableString and NSMutableAttributedString can be used.
[0048] In some embodiments, according to a predefined key-value pair rule, a corresponding key-value pair is set for each source string, including:
[0049] Pass the string corresponding to the feature key as a parameter into the class initialization function of the key of the feature string object to obtain the initialized feature key;
[0050] Set a corresponding feature value for each source string according to the preset data type, and generate a key-value pair corresponding to the source string according to the feature key and the feature value;
[0051] The key-value pair rule includes that each source string shares a characteristic key, each source string corresponds to its own characteristic value, or each source string corresponds to a characteristic key and a characteristic value.
[0052] Specifically, before marking each source string, the present application first predefines a key-value pair rule to distinguish each source string. The present application embodiment provides two key-value pair rules. The contents of these two key-value pair rules are described in detail below in conjunction with specific embodiments, which may include the following contents:
[0053] There is only one attribute key in the first key-value pair rule, but there are multiple attribute values. In the first key-value pair rule, the key is defined as a static constant of type NSAttributedString.Key. Its initialization function init(_:) is called, and a special string (the string corresponding to the attribute key, that is, the key itself) is passed in as a parameter. There is no restriction on the content of the string, as long as it can be distinguished from the style keys provided by the system and other style keys customized in the App. Each source string corresponds to a characteristic value. In principle, the type and value of the characteristic value can be of any type and any value as long as they can be distinguished. For example, basic data types such as int or string can be used.
[0054] In a specific example, source string alpha + source string beta + source string delta = result string alphabeta delta; the key-value pair is set for the above source string using the first key-value pair rule, where the attribute key is represented as: NSAttributedString.Key("key"); the attribute value is represented as: source string alpha corresponds to 1, source string beta corresponds to 2, and source string delta corresponds to 3.
[0055] The second key-value pair rule has multiple attribute keys and multiple attribute values. The attribute keys and attribute values form a key-value pair, and each source string corresponds to a key-value pair. In the second key-value pair rule, the key is defined as multiple static constants. The type of the constant is NSAttributedString. Key, call its initialization function init(_:), and pass a special string as a parameter. There is no restriction on the content of the string, as long as it can be distinguished from the style keys provided by the system and the keys of other source strings. Each attribute value corresponds to a source string. In principle, the type and value of the value can be any type and any value, as long as they can be distinguished. For example, basic data types such as int or string can be used.
[0056] In a specific example, source string alpha + source string beta + source string delta = result string alphabeta delta; the second key-value pair rule is used to set the key-value pair for the above source string, where the attribute key is represented as: source string alpha corresponds to NSAttributedString.Key("key1"), source string beta corresponds to NSAttributedString.Key("key2"), and source string delta corresponds to NSAttributedString.Key("key3"); the attribute value is represented as: source string alpha corresponds to 1, source string beta corresponds to 2, and source string delta corresponds to 3.
[0057] It should be noted that each source string mentioned in the embodiments of the present application does not refer to every source string. There is the following difference between the two: if two source strings have different sources but the final operation is the same, they should be regarded as belonging to the same source string. For example: the source string alpha and the source string beta in the above example, although they have different sources, are finally processed in bold and black, so they should be regarded as the same source string and use one key-value pair. However, if you do not know which source strings will be processed in the final process, and do not know what the final process will be, you should temporarily divide the key-value pairs by source string, and make appropriate adjustments when the final process arrives.
[0058] In some embodiments, storing the source string as a property string object includes:
[0059] The source string is passed as a parameter to the initialization function of the attribute string object, and the attribute string object is generated using the initialization function of the attribute string object.
[0060] Specifically, before the concatenation operation, each source string needs to be marked so that their respective characteristics are retained in the final concatenation result. First, the source string is stored using the attribute string object NSAttributedString. Then the NSAttributedString initialization function is called, and the source string is passed as a parameter to create an instance of the NSAttributedString class, which is called source1 here. NSAttributedString is a class that stores text with attributes. It can add style and other information to the text.
[0061] In actual applications, for the methods of the immutable NSAttributedString and its corresponding subclass NSMutableAttributedString, the tags can be added while generating instances (i.e., attribute string objects). Therefore, for NSAttributedString and its subclass NSMutableAttributedString, the initialization operation and the tag addition operation are performed simultaneously and belong to the same step.
[0062] In some embodiments, tagging a property string object with a key-value pair of a source string includes:
[0063] Generate a dictionary using the key-value pairs of the source string, call the tag adding method corresponding to the feature string object, and pass the dictionary as a parameter to the tag adding method so that the tag adding method adds tags to the feature string object based on the dictionary.
[0064] Specifically, add a tag to the instance source1, that is, add a style to the instance source1, and the key and value of the style are defined by the constants in the previous embodiment. Call the addAttributes(_:) method of the instance source1 (this method is a method of NSMutableAttributedString), which accepts a dictionary as a parameter. Therefore, it is necessary to create a dictionary. The embodiment of the present application generates a dictionary using the attribute keys and attribute values defined in the previous embodiment, and then passes the dictionary into the parameters of the addAttributes(_:) method.
[0065] In actual applications, since NSAttributedString has a more convenient initialization method variant, you can directly call the NSAttributedString initialization function variant init(string:, attributes:), which accepts two parameters, the first parameter is the source string, and the second parameter is the generated dictionary. In this way, you can directly add tags when creating an NSAttributedString instance.
[0066] It should be noted that since NSMutableAttributedString is a subclass of NSAttributedString, it has the methods of the parent class, and the traversal method is a method of the parent class NSAttributedString. NSAttributedString is immutable, so it cannot add tags after initialization, but it can add variables during initialization; NSMutableAttributedString is mutable, so it can add tags after initialization, but it can also add variables during initialization.
[0067] Therefore, in actual applications, the initialization and marking operations of the embodiments of the present application include two implementation scenarios. The first scenario is: for the methods of NSAttributedString and its subclass NSMutableAttributedString, the mark is added while generating the instance; the second scenario is: first initialize the source string into an instance (that is, the attribute string object), and then add the mark to the attribute string object.
[0068] In some embodiments, a concatenation operation is performed on a source string using a preset concatenation method to obtain a concatenated result string, including:
[0069] The add, delete, modify and insert functions of the feature string object or the subclass corresponding to the feature string object are called to concatenate the source strings to obtain a concatenated result string, wherein the type of the result string is the feature string object, the subclass of the feature string object or the structure.
[0070] Specifically, after all source strings are marked, string concatenation can begin. The embodiment of the present application completes the source string concatenation operation by calling the corresponding addition, deletion, modification and insertion functions of the attribute string object NSAttributedString and its variants. In practical applications, source string concatenation is usually completed by operating the relevant methods of NSString or NSAttributedString and its subclasses. For ordinary strings, you can use the stringByAppendingString: and stringByAppendingFormat: methods of NSString to achieve concatenation; for strings with attributes, you can use the append(_:) method of NSMutableAttributedString to achieve concatenation.
[0071] In iOS, string concatenation is usually done by operating the related methods of the ordinary string object NSString or the attribute string object NSAttributedString and its subclasses. Since NSAttributedString is actually an NSString with attributes (i.e. style or pattern), they can be freely converted to each other. This means that in general, any string concatenation operation (addition, deletion, modification and insertion) that can be done using NSString and its subclasses can be implemented through NSAttributedString and its subclasses.
[0072] Based on the above reasons, any string concatenation operation performed on iOS can be implemented through the technical solution of the embodiment of the present application. In addition, in the existing business logic, it may be necessary to stylize the string, so the source string may have been constructed by NSAttributedString and its subclasses, and various system styles have been added. Since the characteristic key values in the technical solution of the present application are distinguished from the system characteristic key values, adding a set of customized key-value pairs to the source string will not cause conflicts.
[0073] Furthermore, the type of the result string generated after the concatenation operation may be an attribute string object NSAttributedString or its variants NSMutableAttributedString or AttributedString.
[0074] The above embodiments specifically introduce the marking process of the source string. In the subsequent processing stage, the source string is extracted according to the previously added mark, and corresponding operations are performed according to business needs, including but not limited to adjusting the style of the string, adding, deleting, modifying, etc. For example, the corresponding mark can be obtained by calling the enumeration attribute function (enumerateAttributes function or enumerateAttribute function) on the result string, and it is passed to the processing function (i.e., callback function), and according to different marking schemes, the most appropriate enumeration attribute function variant needs to be selected to reduce the number of loop traversals. In practical applications, the enumerateAttributes function or the enumerateAttribute function can also be called an enumeration attribute function or a traversal attribute function. This function is used to traverse specific attributes (i.e., characteristics) in an attributed string (NSAttributedStri ng) and perform corresponding processing operations on attributes that meet the conditions.
[0075] In some embodiments, the enumerated attribute function includes a first enumerated attribute function and a second enumerated attribute function;
[0076] When the first enumeration attribute function is used, the source string is extracted and processed, including:
[0077] The feature key to be extracted is used as the first parameter of the first enumeration attribute function, the search range is used as the second parameter of the first enumeration attribute function, the configuration item is used as the third parameter of the first enumeration attribute function, and the callback function is used as the fourth parameter of the first enumeration attribute function;
[0078] Based on the search range, the source string corresponding to the first parameter is searched in the result string, and the characteristic value corresponding to the found source string is passed as a parameter to the callback function, and the callback function is used to extract and process the source string of the characteristic value.
[0079] Specifically, the first enumeration attribute function enumerateAttribute(_:in:options:using:) contains 4 parameters, namely parameter 1: a value of type NSAttributedString.Key, indicating the attribute key to be found; parameter 2: a range, indicating the range to be searched in the result string; parameter 3: configuration item, optional, empty by default; parameter 4: callback function (also called processing function), which accepts three parameters: the attribute value found, the range of the string found, and whether to stop the loop.
[0080] Furthermore, the first enumeration attribute function searches for string information with the attribute key of parameter 1 in the result string within the given search range. After finding it, the callback function of parameter 4 is called. This means that the callback function will only be called when the source string with the attribute key of parameter 1 is found. In the callback function of parameter 4, the attribute values found are filtered to keep only the required attribute values, and then the source string is obtained using the found string range and processed accordingly.
[0081] That is to say, in the subsequent processing stage, the embodiment of the present application can extract the source string according to the added tag by calling the first enumeration attribute function enumerateAttribute(_:in:options:using:) and passing the callback function, and perform corresponding operations on the source string according to business needs.
[0082] In some embodiments, when the second enumeration attribute function is used, extracting the source string includes:
[0083] The search range is used as the first parameter of the second enumeration attribute function, the configuration item is used as the second parameter of the second enumeration attribute function, and the callback function is used as the third parameter of the second enumeration attribute function;
[0084] Based on the search range, the source string with the key-value pair in the result string is searched, and the key-value pair corresponding to the found source string is passed as a parameter to the callback function, and the callback function is used to extract and process the source string of the key-value pair.
[0085] Specifically, the second enumeration attribute function enumerateAttributes(in:, options:, using:) accepts three parameters, namely parameter 1 is a search range, parameter 2 is a configuration item, which is not passed by default, and parameter 3 is a callback function (also called a processing function), where the processing function accepts three parameters: the tag key-value pair found, the range found, and whether to stop the loop.
[0086] Furthermore, the second enumeration attribute function searches for all the features in the string within a certain search range and processes them using the processing logic of parameter 4. That is, each time the second enumeration attribute function finds a feature, it will enter the callback function of parameter 4. In the callback function of parameter 4, the tag key-value pairs found later are used for screening, and the unnecessary feature key-value pairs are discarded. Only the necessary feature key-value pairs are taken out, and the source string is obtained for processing using the range of the found string.
[0087] In combination with the two enumeration attribute functions provided in the above-mentioned embodiment, the selection of the enumeration attribute function under different key-value pair rules and the extraction process of the source character string are described in detail below.
[0088] In a specific example, assume that the resulting string after concatenation is: source string alpha + source string beta + source string delta = result string alphabeta delta; if the first key-value pair rule is used, then when concatenating the strings, the key is "key", and the value of marking each source string is: the mark of the source string alpha is ("key":1), the mark of the source string beta is ("key":2), and the mark of the source string delta is ("key":3).
[0089] In one possible implementation, it is only necessary to extract the source string beta for processing later, such as making beta bold and black. At this time, it is more efficient to call the first enumeration attribute function, pass NSAttributedString.Key("key") in parameter 1 of the first enumeration attribute function, and pass the logic processing function in parameter 4. The logic processing function will be entered multiple times, so it is judged and processed in it: the first parameter of the judgment logic function is the found characteristic value, which may be 1, 2, or 3. In this embodiment, only beta is required to be processed, so only when the value is 2, the second parameter of the logic processing function is taken. The second parameter is the range of the found string. The specific source string "beta" is obtained through the range, and the logic of bolding and blackening is written to it, and the rest is discarded.
[0090] In another possible implementation, the source string alpha and the original string beta need to be taken out for processing later, for example, alpha and beta are processed in bold and black. At this time, the first enumeration attribute function is called to pass the logic processing function in parameter 4. The logic processing function will be entered multiple times, so it is judged and processed in it: the first parameter of the logic processing function is judged, which is the characteristic value found, which may be 1, 2, 3. In this embodiment, only alpha and beta are required to be processed, so the second parameter of the logic processing function is taken only when the value is 1 or 2. The second parameter is the range of the found string. The specific source string "beta" or "alpha" is obtained through the range, and the logic of bolding and blackening is written for it, and the rest is discarded. In this embodiment, if the second key-value pair rule is used, each source string is marked when the string is concatenated, wherein the source string alpha is marked as "A" corresponding to (key1:1), the source string beta is marked as "B" corresponding to (key2:2), and the source string delta is marked as "C" corresponding to (key3:3).
[0091] In another possible implementation, it is only necessary to take out the source string beta for processing later, such as making beta bold and black. At this time, it is more efficient to call the first enumeration attribute function, pass in NSAttributedString.Key("key2") in parameter 1 of the first enumeration attribute function, and pass in the logic processing function in parameter 4. In principle, the logic processing function will be entered multiple times, which means that multiple source strings may be found, and they are all the same source strings. However, there is only one in this embodiment, so the logic processing function will only be entered once. Judge and process it: the first parameter of the logic function, which is the found feature value, in this embodiment it will only be 2, the second parameter of the logic processing function is the range of the found string (in this embodiment, the position is 5 and the length is 4), and the specific source string beta is obtained through the range, and the logic of bolding and blackening is written for it.
[0092] In another possible implementation, the source string alpha and the original string beta need to be processed, for example, alpha and beta are made bold and black. In this case, there are the following two situations:
[0093] (1) The first enumeration attribute function can be called twice
[0094] When the function is called for the first time, pass NSAttributedString.Key("key1") in parameter 1 and pass parameter 4 for logical processing;
[0095] When the function is called for the second time, pass NSAttributedString.Key("key2") in parameter 1 and pass parameter 4 for logical processing.
[0096] (2) You can also call the second enumeration attribute function once, and pass the logic processing function in parameter 4. The logic processing function will be entered multiple times, so it is judged and processed in it: the first parameter of the logic processing function is judged, which is the key-value pair found, which may be (key1:1), (key2:2), (key3:3). In this embodiment, only alpha and beta are required to be processed, so the system will only call the logic processing function when the key-value pairs are (key1:1) and (key2:2); the second parameter of the logic processing function is the range of the found string. The specific source string alpha or beta is obtained through the range, and the logic of bolding is written to it, and the rest is discarded.
[0097] According to the technical solution provided by the embodiment of the present application, the embodiment of the present application sets predefined key-value pair rules for different source strings to distinguish and manage source strings. Through these two key-value pair rules, the source string can be marked, and the source string can be stored through the object of the attribute string class NSAttributedString. In the implementation process, the source string is passed into the initialization function of the attribute string object to generate an instance of the attribute string object. According to the key-value pair of the source string, a tag is added to the attribute string object instance. Before the splicing operation, each source string is marked so that its respective characteristics are retained in the splicing result. The splicing operation can be completed by calling the add, delete, modify and insert functions of the attribute string object NSAttributedString and its subclasses. The technical solution of the present application is applicable to string splicing operations on iOS, and can be compatible with existing business logic to perform stylized processing on the source string. In the subsequent processing stage, the source string is extracted according to the added tag, and corresponding operations are performed according to business needs, such as adjusting the string style, adding, deleting, modifying and inserting, etc. The embodiment of the present application does not require additional variables to store strings, so there is no maintenance cost for the variable life cycle. In the embodiment of the present application, there is no need to record the position, no matter how the source string or the result string changes, it will not be affected. The embodiment of the present application does not have any rule restrictions on the source string. In the embodiment of the present application, the loop traversal is minimized to improve efficiency.
[0098] The following is an embodiment of the device of the present application, which can be used to execute the embodiment of the method of the present application. For details not disclosed in the embodiment of the device of the present application, please refer to the embodiment of the method of the present application.
[0099] Figure 2 Schematic diagram of the structure of the source string concatenation and extraction device provided in the embodiment of the present application. Figure 2 As shown, the source string concatenation and extraction device includes:
[0100] The setting module 201 is configured to set a corresponding key-value pair for each source string according to a predefined key-value pair rule, wherein the key-value pair includes a characteristic key and a characteristic value corresponding to the source string;
[0101] A generating module 202 is configured to store the source string as a characteristic string object and add a tag to the characteristic string object using a key-value pair of the source string;
[0102] The splicing module 203 is configured to perform a splicing operation on the source character string using a preset splicing method to obtain a spliced result character string;
[0103] The extraction module 204 is configured to call a predetermined enumeration attribute function to traverse the result string during post-processing, obtain the marked key-value pairs or characteristic values, use the key-value pairs or characteristic values as input to the callback function, and use the callback function to extract and process the source string corresponding to the key-value pairs or characteristic values.
[0104] In some embodiments, Figure 2 The setting module 201 passes the string corresponding to the feature key as a parameter into the key class initialization function of the feature string object to obtain the initialized feature key; sets the corresponding feature value for each source string according to the preset data type, and generates a key-value pair corresponding to the source string according to the feature key and the feature value; wherein the key-value pair rule includes that each source string shares one feature key, each source string corresponds to its own feature value, or each source string corresponds to a feature key and a feature value.
[0105] In some embodiments, Figure 2 The generating module 202 passes the source string as a parameter into the initialization function of the characteristic string object, and generates the characteristic string object using the initialization function of the characteristic string object.
[0106] In some embodiments, Figure 2 The generation module 202 generates a dictionary using the key-value pairs of the source string, calls a tag adding method corresponding to the characteristic string object, and passes the dictionary as a parameter into the tag adding method, so that the tag adding method adds a tag to the characteristic string object based on the dictionary.
[0107] In some embodiments, Figure 2 The splicing module 203 calls the add, delete, modify and insert function of the characteristic string object or the subclass corresponding to the characteristic string object to splice the source string to obtain a spliced result string, wherein the type of the result string is the characteristic string object, the subclass of the characteristic string object or the structure.
[0108] In some embodiments, the enumerated attribute function includes a first enumerated attribute function and a second enumerated attribute function; Figure 2 When the first enumeration attribute function is adopted, the extraction module 204 uses the feature key to be extracted as the first parameter of the first enumeration attribute function, the search range as the second parameter of the first enumeration attribute function, the configuration item as the third parameter of the first enumeration attribute function, and the callback function as the fourth parameter of the first enumeration attribute function; based on the search range, the source string corresponding to the first parameter in the result string is searched, and the feature value corresponding to the found source string is passed as a parameter to the callback function, and the source string of the feature value is extracted and processed by using the callback function.
[0109] In some embodiments, Figure 2 When the second enumeration attribute function is adopted, the extraction module 204 uses the search range as the first parameter of the second enumeration attribute function, the configuration item as the second parameter of the second enumeration attribute function, and the callback function as the third parameter of the second enumeration attribute function; based on the search range, the source string with the key-value pair in the result string is searched, and the key-value pair corresponding to the found source string is passed into the callback function as a parameter, and the callback function is used to extract and process the source string of the key-value pair.
[0110] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0111] Figure 3 Schematic diagram of the structure of the electronic device 3 provided in the embodiment of the present application. Figure 3 As shown, the electronic device 3 of this embodiment includes: a processor 301, a memory 302, and a computer program 303 stored in the memory 302 and executable on the processor 301. When the processor 301 executes the computer program 303, the steps in the above-mentioned method embodiments are implemented. Alternatively, when the processor 301 executes the computer program 303, the functions of the modules / units in the above-mentioned device embodiments are implemented.
[0112] Exemplarily, the computer program 303 may be divided into one or more modules / units, which are stored in the memory 302 and executed by the processor 301 to complete the present application. The one or more modules / units may be a series of computer program instruction segments capable of completing specific functions, which are used to describe the execution process of the computer program 303 in the electronic device 3.
[0113] The electronic device 3 may be a desktop computer, a notebook, a PDA, a cloud server, or other electronic device. The electronic device 3 may include, but is not limited to, a processor 301 and a memory 302. Those skilled in the art will appreciate that Figure 3 It is only an example of the electronic device 3 and does not constitute a limitation of the electronic device 3. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the electronic device may also include input and output devices, network access devices, buses, etc.
[0114] The processor 301 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc.
[0115] The memory 302 may be an internal storage unit of the electronic device 3, for example, a hard disk or memory of the electronic device 3. The memory 302 may also be an external storage device of the electronic device 3, for example, a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the electronic device 3. Further, the memory 302 may also include both an internal storage unit and an external storage device of the electronic device 3. The memory 302 is used to store computer programs and other programs and data required by the electronic device. The memory 302 may also be used to temporarily store data that has been output or is to be output.
[0116] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.
[0117] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0118] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0119] In the embodiments provided in the present application, it should be understood that the disclosed devices / computer equipment and methods can be implemented in other ways. For example, the device / computer equipment embodiments described above are only schematic. For example, the division of modules or units is only a logical function division. There may be other division methods in actual implementation. Multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0120] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0121] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0122] If the integrated module / unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. The computer program may include computer program code, and the computer program code may be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying computer program code, recording medium, U disk, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, the computer-readable medium does not include electric carrier signals and telecommunication signals.
[0123] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.
Claims
1. A source string concatenation and extraction method, characterized in that: include: According to a predefined key-value pair rule, a corresponding key-value pair is set for each source string, wherein the key-value pair includes a characteristic key and a characteristic value corresponding to the source string; The source string is stored as a characteristic string object, and a tag is added to the characteristic string object using a key-value pair of the source string; Performing a concatenation operation on the source character string using a preset concatenation method to obtain a concatenated result character string; In the post-processing, a predetermined enumeration attribute function is called to traverse the result string to obtain a marked key-value pair or characteristic value, the key-value pair or characteristic value is used as an input of a callback function, and the callback function is used to extract and process the source string corresponding to the key-value pair or characteristic value; The step of setting a corresponding key-value pair for each source string according to a predefined key-value pair rule includes: Pass the string corresponding to the feature key as a parameter into the class initialization function of the key of the feature string object to obtain the initialized feature key; Setting a corresponding characteristic value for each source character string according to a preset data type, and generating a key-value pair corresponding to the source character string according to the characteristic key and the characteristic value; The key-value pair rule includes that each source character string shares a characteristic key, each source character string corresponds to its own characteristic value, or each source character string corresponds to a characteristic key and a characteristic value.
2. The method according to claim 1, characterized in that The storing the source string as a characteristic string object comprises: The source string is passed as a parameter into the initialization function of the characteristic string object, and the characteristic string object is generated using the initialization function of the characteristic string object.
3. The method according to claim 2, characterized in that The step of adding a tag to the characteristic string object by using the key-value pair of the source string includes: A dictionary is generated using the key-value pairs of the source string, a tag adding method corresponding to the characteristic string object is called, and the dictionary is passed into the tag adding method as a parameter, so that the tag adding method adds a tag to the characteristic string object based on the dictionary.
4. The method according to claim 1, characterized in that The method of performing a splicing operation on the source character string using a preset splicing method to obtain a spliced result character string includes: The add, delete, modify and insert function of the characteristic string object or the subclass corresponding to the characteristic string object is called to concatenate the source string to obtain a concatenated result string, wherein the type of the result string is the characteristic string object, the subclass of the characteristic string object or the structure.
5. The method according to claim 1, characterized in that The enumerated attribute function includes a first enumerated attribute function and a second enumerated attribute function; When the first enumeration attribute function is used, the source character string is extracted and processed, including: The characteristic key to be extracted is used as the first parameter of the first enumeration attribute function, the search range is used as the second parameter of the first enumeration attribute function, the configuration item is used as the third parameter of the first enumeration attribute function, and the callback function is used as the fourth parameter of the first enumeration attribute function; Based on the search range, the source string in the result string that has a corresponding relationship with the first parameter is searched, and the characteristic value corresponding to the found source string is passed as a parameter to the callback function, and the source string of the characteristic value is extracted and processed using the callback function.
6. The method according to claim 5, characterized in that When the second enumeration attribute function is used, the source character string is extracted, including: Using the search range as the first parameter of the second enumeration attribute function, using the configuration item as the second parameter of the second enumeration attribute function, and using the callback function as the third parameter of the second enumeration attribute function; Based on the search range, the source string with the key-value pair in the result string is searched, and the key-value pair corresponding to the found source string is passed as a parameter to the callback function, and the source string of the key-value pair is extracted and processed by the callback function.
7. A source string concatenation and extraction device, characterized in that: include: A setting module is configured to set a corresponding key-value pair for each source string according to a predefined key-value pair rule, wherein the key-value pair includes a characteristic key and a characteristic value corresponding to the source string; A generating module, configured to store the source string as a characteristic string object, and to add a tag to the characteristic string object using a key-value pair of the source string; A splicing module is configured to perform a splicing operation on the source character string using a preset splicing method to obtain a spliced result character string; The extraction module is configured to call a predetermined enumeration attribute function to traverse the result string during post-processing to obtain a marked key-value pair or characteristic value, use the key-value pair or characteristic value as an input of a callback function, and use the callback function to extract and process a source string corresponding to the key-value pair or characteristic value; Among them, the setting module is used to pass the string corresponding to the feature key as a parameter into the class initialization function of the key of the feature string object to obtain the initialized feature key; set the corresponding feature value for each source string according to the preset data type, and generate the key-value pair corresponding to the source string according to the feature key and the feature value; wherein the key-value pair rule includes that each source string shares a feature key, each source string corresponds to its own feature value, or each source string corresponds to a feature key and a feature value.
8. An electronic device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor implements the method according to any one of claims 1 to 6 when executing the program.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.
