📄 string.cpp
字号:
/************************************************************************* * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * *************************************************************************/#include "string.h" /** * Wraps str at width width, or at the best suitable position. * * @param str The string that should be wrapped. * @param width The maximal column count of the wrapped string. * @returns The wrapped string. */string String::wrap (string str, unsigned int width){ unsigned int idx = 0; unsigned int last_space = 0; unsigned int col = 0; while (idx < str.length ()) { if (str[idx] == ' ') { last_space = idx; } if (++col == width) { str[last_space] = '\n'; col = 0; } if (str[idx] == '\n') { col = 0; } idx++; } return (str);}/** * Returns str in lower case characters. * * @param str The string that should be lowerized. * @returns str in lower cases. */string String::to_lower (string str){ for (unsigned int idx = 0; idx < str.length (); idx++) { str[idx] = tolower (str[idx]); } return (str);}/** * Returns str in upper case characters. * * @param str The string that should be upperized. * @returns str in upper cases. */string String::to_upper (string str){ for (unsigned int idx = 0; idx < str.length (); idx++) { str[idx] = toupper (str[idx]); } return (str);}/** * Returns str as a vector of its lines. * * @param str The string that should be split up into lines. * @returns All lines of str grouped into a vector. */vector<string> String::string2lines (const string &str){ vector<string> str_lines; unsigned int pos = str.find ("\n"); unsigned int old_pos = 0; while (pos != string::npos) { string line = str.substr (old_pos, (pos-old_pos)); str_lines.push_back (line); old_pos = pos; pos = str.find ("\n", ++old_pos); } if (old_pos < (str.length ()-1)) { str_lines.push_back (str.substr (old_pos)); } return (str_lines);}/** * This method parses the string str into a vector * of its tokens. The delimiting character is a single * blankspace, so if you want usefull results call String::trim() * before. * * @param str The string that should be tokenized. * @returns All tokens of str grouped into a vector. */vector<string> String::string2tokens (const string &str){ return (String::string2tokens (str, ' '));}/** * This method parses the string str into a vector * of its tokens. * * @param str The string that should be tokenized. * @param delim A single character that should be used as * a delimiter. * @returns All tokens of str grouped into a vector. */vector<string> String::string2tokens (const string &str, char delim){ vector<string> str_tokens; unsigned int pos = str.find (delim); unsigned int old_pos = 0; while (pos != string::npos) { string token = str.substr (old_pos, (pos-old_pos)); str_tokens.push_back (token); old_pos = pos; pos = str.find (delim, ++old_pos); } if (old_pos < (str.length ()-1)) { str_tokens.push_back (str.substr (old_pos)); } return (str_tokens);}/** * Removes all leading and tailing blankspaces, and * replaces all multiple occurences of blank spaces * with a single one. * eg: * " this is a test string" * => "this is a test string" * * @param str The string that should be trimmed. * @returns The trimmed string. */string String::trim (string str){ unsigned int pos; while ((pos = str.find (" ")) != string::npos) { str.replace (pos, 2, " "); } if (str[0] == ' ') { str.replace (0, 1, ""); } if (str[str.length ()-1] == ' ') { str.replace (str.length ()-1, 1, ""); } return (str);}/** * Converts an integer to the radix 10 into a string. * * @param i The integer that should be converted to a string. * @returns A string representation of i. */string String::itoa (int i){ char buf[16]; sprintf (buf, "%d", i); return (string (buf));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -