dart Flutter -删除某个字符后的字符串?

aiazj4mn  于 2024-01-03  发布在  Flutter
关注(0)|答案(7)|浏览(203)

在Flutter中删除String对象中特定字符之后的所有字符的最佳方法是什么?
假设我有以下字符串:
one.two
我需要把“. 2”去掉,怎么做
先谢了。

xxhby3vn

xxhby3vn1#

可以使用String类中的subString方法

  1. String s = "one.two";
  2. //Removes everything after first '.'
  3. String result = s.substring(0, s.indexOf('.'));
  4. print(result);

字符串
如果String中有多个“.”,它将使用第一次出现的“.”。如果您需要使用最后一个“.”(例如,为了摆脱文件扩展名),请将indexOf更改为lastIndexOf。如果您不确定至少有一个“.”,则还应添加一些验证以避免触发异常。

  1. String s = "one.two.three";
  2. //Remove everything after last '.'
  3. var pos = s.lastIndexOf('.');
  4. String result = (pos != -1)? s.substring(0, pos): s;
  5. print(result);

展开查看全部
iqjalb3h

iqjalb3h2#

  1. void main() {
  2. String str = "one.two";
  3. print(str.replaceAll(".two", ""));
  4. // or
  5. print(str.split(".").first);
  6. // or
  7. String newStr = str.replaceRange(str.indexOf("."), str.length, "");
  8. print(newStr);
  9. // Lets do a another example
  10. String nums = "1,one.2,two.3,three.4,four";
  11. List values = nums.split("."); // split() will split from . and gives new List with separated elements.
  12. values.forEach(print);
  13. //output
  14. // 1,one
  15. // 2,two
  16. // 3,three
  17. // 4,four
  18. }

字符串
DartPad中编辑此内容。
实际上,在String中还有其他很酷的方法。看看那些here

展开查看全部
p4rjhz4m

p4rjhz4m3#

  1. String str = "one.two";
  2. var value = str?.replaceFirst(RegExp(r"\.[^]*"), "");

字符串
如果确定str包含.,则可以使用str.substring(0, str.indexOf('.'));
否则,您将得到错误Value not in range: -1

iqxoj9l9

iqxoj9l94#

答案在上面,但这里是你如何做到这一点,如果你想只有某些字符的位置或位置。
为了从Dart String中获取子字符串,我们使用substring()方法:

  1. String str = 'bezkoder.com';
  2. // For example, here we want ‘r’ is the ending. In ‘bezkoder.com’,
  3. // the index of ‘r’ is 7. So we need to set endIndex by 8.
  4. str.substring(0,8); // bezkoder
  5. str.substring(2,8); // zkoder
  6. str.substring(3); // koder.com

字符串
这是返回String的substring()方法的签名:

  1. String substring(int startIndex, [int endIndex]);


startIndex:开始字符的索引。开始索引为0。endIndex(可选):结束字符的索引+ 1。如果不设置,结果将是从startIndex开始到字符串结尾的减法。
[参考] [1]:https://bezkoder.com/dart-string-methods-operators-examples/

展开查看全部
sdnqo3pr

sdnqo3pr5#

  1. String str = "one.two";
  2. print(str.replaceAll(".two", ""));

字符串

wz8daaqr

wz8daaqr6#

这里是一个完整的工作代码,结合了所有上述解决方案
也许我的语法编写和分割代码是不常见的,,对不起,我自学初学者

  1. // === === === === === === === === === === === === === === === === === === ===
  2. import 'dart:io';
  3. import 'package:flutter/services.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. /// in case you are saving assets in flutter project folder directory like this
  6. /// 'assets/xyz/somFolder/image.jpg'
  7. /// And u have a separate class where u save assets in variables like this to easily call them in your widget tree
  8. ///
  9. /// class SuperDuperAsset {
  10. /// static const String DumAuthorPic = 'assets/dum/dum_author_pic.jpg' ;
  11. /// static const String DumBusinessLogo = 'assets/dum/dum_business_logo.jpg' ;
  12. /// }
  13. ///
  14. /// I just want to insert SuperDuperAsset.DumBusinessLogo in this function below
  15. ///
  16. /// and here is a fix and a combination for all mentioned solutions that works like a charm in case you would like to copy paste it
  17. /// and just use this function like this
  18. ///
  19. /// File _imageFile = await getImageFileFromAssets(SuperDuperAsset.DumBusinessLogo);
  20. /// and you are good to go
  21. ///
  22. /// some other functions that manipulate the asset path are separated below
  23. Future<File> getImageFileFromAssets(String asset) async {
  24. String _pathTrimmed = removeNumberOfCharacterFromAString(asset, 7);
  25. final _byteData = await rootBundle.load('assets/$_pathTrimmed');
  26. final _tempFile = File('${(await getTemporaryDirectory()).path}/${getFileNameFromAsset(_pathTrimmed)}');
  27. await _tempFile.writeAsBytes(_byteData.buffer.asUint8List(_byteData.offsetInBytes, _byteData.lengthInBytes));
  28. _tempFile.create(recursive: true);
  29. return _tempFile;
  30. }
  31. // === === === === === === === === === === === === === === === === === === ===
  32. String removeNumberOfCharacterFromAString(String string, int numberOfCharacters){
  33. String _stringTrimmed;
  34. if (numberOfCharacters > string.length){
  35. print('can not remove ($numberOfCharacters) from the given string because : numberOfCharacters > string.length');
  36. throw('can not remove ($numberOfCharacters) from the given string because');
  37. } else {
  38. _stringTrimmed = string.length >0 ? string?.substring(numberOfCharacters) : null;
  39. }
  40. return _stringTrimmed;
  41. }
  42. // === === === === === === === === === === === === === === === === === === ===
  43. /// this trims paths like
  44. /// 'assets/dum/dum_business_logo.jpg' to 'dum_business_logo.jpg'
  45. String getFileNameFromAsset(String asset){
  46. String _fileName = trimTextBeforeLastSpecialCharacter(asset, '/');
  47. return _fileName;
  48. }
  49. // === === === === === === === === === === === === === === === === === === ===
  50. String trimTextBeforeLastSpecialCharacter(String verse, String specialCharacter){
  51. int _position = verse.lastIndexOf(specialCharacter);
  52. String _result = (_position != -1)? verse.substring(_position+1, verse.length): verse;
  53. return _result;
  54. }
  55. // === === === === === === === === === === === === === === === === === === ===

字符串

展开查看全部
o0lyfsai

o0lyfsai7#

  1. var regex = RegExp(r'.[a-z]*$');
  2. String s='one.two';
  3. print(s.replaceAll(regex,''));

字符串

相关问题