1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "contacts_string_utils.h"
17 
18 namespace OHOS {
19 namespace Contacts {
ContactsStringUtils()20 ContactsStringUtils::ContactsStringUtils()
21 {
22 }
23 
~ContactsStringUtils()24 ContactsStringUtils::~ContactsStringUtils()
25 {
26 }
27 
SplitStr(std::string str, std::string spSymbol)28 std::vector<std::string> ContactsStringUtils::SplitStr(std::string str, std::string spSymbol)
29 {
30     std::vector<std::string> result;
31     std::string::size_type i = 0;
32     Trim(str);
33     std::string::size_type found = str.find(spSymbol);
34     if (found != std::string::npos) {
35         while (found != std::string::npos) {
36             std::string value = str.substr(i, found - i);
37             Trim(value);
38             if (!value.empty()) {
39                 result.push_back(value);
40             }
41             i = found + spSymbol.size();
42             found = str.find(spSymbol, i);
43         }
44         std::string lastv = str.substr(i, str.size() - i);
45         Trim(lastv);
46         if (!lastv.empty()) {
47             result.push_back(lastv);
48         }
49     }
50     return result;
51 }
52 
Trim(std::string &str)53 std::string &ContactsStringUtils::Trim(std::string &str)
54 {
55     if (str.empty()) {
56         return str;
57     }
58     str.erase(0, str.find_first_not_of(" "));
59     str.erase(str.find_last_not_of(" ") + 1);
60     return str;
61 }
62 } // namespace Contacts
63 } // namespace OHOS