{"action":"opened","issue":{"url":"https://api.github.com/repos/arduino/Arduino/issues/4870","repository_url":"https://api.github.com/repos/arduino/Arduino","labels_url":"https://api.github.com/repos/arduino/Arduino/issues/4870/labels{/name}","comments_url":"https://api.github.com/repos/arduino/Arduino/issues/4870/comments","events_url":"https://api.github.com/repos/arduino/Arduino/issues/4870/events","html_url":"https://github.com/arduino/Arduino/issues/4870","id":148706411,"number":4870,"title":"New functionality in String class (WString.cpp/.h)","user":{"login":"TobiasKnauss","id":18487427,"avatar_url":"https://avatars.githubusercontent.com/u/18487427?v=3","gravatar_id":"","url":"https://api.github.com/users/TobiasKnauss","html_url":"https://github.com/TobiasKnauss","followers_url":"https://api.github.com/users/TobiasKnauss/followers","following_url":"https://api.github.com/users/TobiasKnauss/following{/other_user}","gists_url":"https://api.github.com/users/TobiasKnauss/gists{/gist_id}","starred_url":"https://api.github.com/users/TobiasKnauss/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TobiasKnauss/subscriptions","organizations_url":"https://api.github.com/users/TobiasKnauss/orgs","repos_url":"https://api.github.com/users/TobiasKnauss/repos","events_url":"https://api.github.com/users/TobiasKnauss/events{/privacy}","received_events_url":"https://api.github.com/users/TobiasKnauss/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-04-15T16:28:38Z","updated_at":"2016-04-15T16:28:38Z","closed_at":null,"body":"I need some new functions in the the String class.\r\nI had created 2 posts about that in the arduino developers google group some hours ago, but the posts never appeared. Maybe the moderator is currently N/A...?\r\n\r\nWhile waiting for the posts to appear, I have created the functionality and tested it.\r\nIt's ready to use and I'd be happy to see it implemented in the official version of the library:\r\n\r\n```\r\nWString.h:\r\n  void trim(void);\r\n  void trim(char remove);\r\n  void trimStart(void);\r\n  void trimStart(char remove);\r\n  void trimEnd(void);\r\n  void trimEnd(char remove);\r\n  void padLeft(unsigned int newlength);\r\n  void padLeft(unsigned int newlength, char add);\r\n  void padRight(unsigned int newlength);\r\n  void padRight(unsigned int newlength, char add);\r\n  void cropLeft(unsigned int count);\r\n  void cropRight(unsigned int count);\r\n  void keepLeft(unsigned int count);\r\n  void keepRight(unsigned int count);\r\n\r\nWString.cpp:\r\nvoid String::trim(void)\r\n{\r\n  trim (0x20);\r\n}\r\n\r\nvoid String::trim(char remove)\r\n{\r\n  trimStart(remove);\r\n  trimEnd  (remove);\r\n}\r\n\r\nvoid String::trimStart(void)\r\n{\r\n  trimStart(0x20);\r\n}\r\n\r\nvoid String::trimStart(char remove)\r\n{\r\n  if (!buffer || len == 0 || remove == 0) return;\r\n  char *begin = buffer;\r\n  while ((*begin) == remove) begin++;\r\n  len = buffer + len - begin;\r\n  if (begin > buffer) memmove(buffer, begin, len);\r\n  buffer[len] = 0;\r\n}\r\n\r\nvoid String::trimEnd(void)\r\n{\r\n  trimEnd(0x20);\r\n}\r\n\r\nvoid String::trimEnd(char remove)\r\n{\r\n  if (!buffer || len == 0 || remove == 0) return;\r\n  char *end = buffer + len - 1;\r\n  while ((*end) == remove && end >= buffer) end--;\r\n  len = end + 1 - buffer;\r\n  buffer[len] = 0;\r\n}\r\n\r\nvoid String::padLeft(unsigned int newlength)\r\n{\r\n  padLeft(newlength, 0x20);\r\n}\r\n\r\nvoid String::padLeft(unsigned int newlength, char add)\r\n{\r\n  if (newlength < len) newlength = len;\r\n  if (!reserve(newlength)) return;\r\n  unsigned int lenAdd = newlength - len;\r\n  char *begin = buffer + lenAdd;\r\n  memmove (begin, buffer, len);\r\n  memset (buffer, add, lenAdd);\r\n  len = newlength;\r\n  buffer[len] = 0;\r\n}\r\n\r\nvoid String::padRight(unsigned int newlength)\r\n{\r\n  padRight(newlength, 0x20);\r\n}\r\n\r\nvoid String::padRight(unsigned int newlength, char add)\r\n{\r\n  if (newlength < len) newlength = len;\r\n  if (!reserve(newlength)) return;\r\n  unsigned int lenAdd = newlength - len;\r\n  char *begin = buffer + len;\r\n  memset (begin, add, lenAdd);\r\n  len = newlength;\r\n  buffer[len] = 0;\r\n}\r\n\r\nvoid String::cropLeft(unsigned int count)\r\n{\r\n  unsigned int lenKeep = len - count;\r\n  keepRight(lenKeep);\r\n}\r\n\r\nvoid String::cropRight(unsigned int count)\r\n{\r\n  unsigned int lenKeep = len - count;\r\n  keepLeft(lenKeep);\r\n}\r\n\r\nvoid String::keepLeft(unsigned int count)\r\n{\r\n  if (!buffer || len == 0) return;\r\n  if (count <= len) len = count;\r\n  buffer[len] = 0;\r\n}\r\n\r\nvoid String::keepRight(unsigned int count)\r\n{\r\n  if (!buffer || len == 0) return;\r\n  if (count > len) count = len;\r\n  char *begin = buffer + len - count;\r\n  len = count;\r\n  if (begin > buffer) memmove(buffer, begin, len);\r\n  buffer[len] = 0;\r\n}\r\n```\r\n"}}