1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-11 13:10:58 +00:00

Merge pull request #18176 from nikibobi/string-trim

Add string trim_prefix, trim_suffix, lstrip and rstrip methods
This commit is contained in:
George Marques
2018-04-22 12:29:44 -03:00
committed by GitHub
4 changed files with 100 additions and 0 deletions

View File

@@ -2987,6 +2987,40 @@ String String::strip_escapes() const {
return substr(beg, end - beg);
}
String String::lstrip(const Vector<CharType> &p_chars) const {
int len = length();
int beg;
for (beg = 0; beg < len; beg++) {
if (p_chars.find(operator[](beg)) == -1)
break;
}
if (beg == 0)
return *this;
return substr(beg, len - beg);
}
String String::rstrip(const Vector<CharType> &p_chars) const {
int len = length();
int end;
for (end = len - 1; end >= 0; end--) {
if (p_chars.find(operator[](end)) == -1)
break;
}
if (end == len - 1)
return *this;
return substr(0, end + 1);
}
String String::simplify_path() const {
String s = *this;
@@ -3438,6 +3472,24 @@ String String::pad_zeros(int p_digits) const {
return s;
}
String String::trim_prefix(const String &p_prefix) const {
String s = *this;
if (s.begins_with(p_prefix)) {
return s.substr(p_prefix.length(), s.length() - p_prefix.length());
}
return s;
}
String String::trim_suffix(const String &p_suffix) const {
String s = *this;
if (s.ends_with(p_suffix)) {
return s.substr(0, s.length() - p_suffix.length());
}
return s;
}
bool String::is_valid_integer() const {
int len = length();