从开始或结束修剪Dart字符串中的空格 [英] Trimming whitespace in Dart strings from beginning or end

查看:445
本文介绍了从开始或结束修剪Dart字符串中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Dart的新手,我试图得到一些基本的图书馆的感觉。对于字符串,提供了一个trim()函数。这是好的,但没有明显的方法来修剪空格只在开始或只是在字符串的结尾?我找不到他们。谢谢你。

I am very new to Dart and am trying to get some sense of the basic libraries. For strings, there is a trim() function provided. This is good, but are there no obvious ways to trim whitespace only at the beginning or only at the end of a string? I cannot find them.Thank you.

推荐答案

没有特定的方法只修剪前导或尾随空格。但它很容易实现它们:

There are no specific methods for trimming only leading or trailing whitespace. But it is quite easy to implement them:

/// trims leading whitespace
String ltrim(String str) {
  return str.replaceFirst(new RegExp(r"^\s+"), "");
}

/// trims trailing whitespace
String rtrim(String str) {
  return str.replaceFirst(new RegExp(r"\s+$"), "");
}

这篇关于从开始或结束修剪Dart字符串中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆