How can I manipulate a string in dart? - flutter

Currently I'm working in a project with flutter, but I realize there is a need in the management of the variables I'm using.
Basically I want to delete the last character of a string I'm concatenating, something like this:
string varString = 'My text'
And with the help of some method or function, the result I get:
'My tex'
Am I clear about it? I'm looking for some way which helps me to 'pop' the last character of a text (like pop function in javascript)
Is there something like that? I search in the Dart docs, but I didn't find anything about it.
Thank you in advance.

You can take a substring, like this:
string.substring(0, string.length - 1)
If you need the last character before popping, you can do this:
string[string.length - 1]
Strings in dart are immutable, so the only way to do the operation you are describing is by constructing a new instance of a string, as described above.

var str = 'My text';
var newStr = (str.split('')..removeLast()).join();
print(newStr);
Another way:
var newStr2 = str.replaceFirst(RegExp(r'.$') , '');
print(newStr2);

Related

cant translate text with value using GETX in flutter

the problem is that the text has value which declares the day before the text
so idk how to translate this text that includes value.
untilEventDay =
'${pDate.difference(DateTime.now()).inDays},days/ until event day'
.tr;
in translation page :
,days/ until next event day': 'ڕؤژ ماوه‌/ تاوه‌كو ئیڤێنتی داهاتوو',
you should separate the value's string from your translation
var eventDayCountDownTitle = '${pDate.difference(DateTime.now()).inDays}' + ',' + days/ until event day'.tr;
and if you need your day number to be in a specific language, you can use a map or a helper method. map solution would be something like this:
Map<String,String> englishToPersianNumber = {'1' : '۱'}
and then use it in your string :
englishToPersianNumber[pDate.difference(DateTime.now()).inDays.toString()]
Important: to have a cleaner code, you can create a helper method to generate your desired string, and call it in your text widget. the code would be more understandable that way. Also, you can add handle any conditions that may later be added to the string generator. like if it's the last day, write something else instead of 0 days remaining.
String eventDayCountDownTitle(int remainingDays) {
if(remainingDays == 0) return "Less than One day to the event".tr;
return '${remainingDays.toString}' + ',' + 'days/ until event day'.tr;
}
ps. your question's title is wrong, you should change it to what you're explaining in the caption

How to construct a unicode glyph key in C#

I am using FontAwesome to display glyphs in my Xamarin Android application. If I hardcode the glyph like this, where everything works fine:
string iconKey = "\uf0a3";
var drawable = new IconDrawable(this.Context, iconKey, "Font Awesome 5 Pro-Regular-400.otf").Color(Xamarin.Forms.Color.White.ToAndroid()).SizeDp(fontSize);
However, if what I have is the four letter code "f0a3" from FontAwesome's cheatsheet, stored in a string variable, I don't know how to set my iconKey variable to a value that works. Just concatenating a "\u" onto the beginning doesn't work, which makes sense since that's a Unicode escape indicator, not part of a standard string, but I don't know what to do instead. I also tried converting to and from Unicode in various random ways - e.g.
iconKey = unicode.GetChars(unicode.GetBytes("/u" + myFourChar.ToString())).ToString();
but unsurprisingly that didn't work either.
The IconDrawable is from here. The value I send becomes an input there to the Paint.GetTextBounds method and the Canvas.DrawText method.
Thanks for any assistance!
Found the answer here. Here is the code I am using, based on that post but simplified since I have only one hexadecimal character to handle:
string myString = "f0a3";
var chars = new char[] { (char)Convert.ToInt32(myString, 16) };
string iconKey = new string(chars);
var drawable = new IconDrawable(this.Context, iconKey, "Font Awesome 5 Pro-Regular-400.otf").Color(Xamarin.Forms.Color.White.ToAndroid()).SizeDp(fontSize);

Dart convert dynamic to set

I currently have data like this:
_classes = _items.map((e) => e.ClassID).toSet();
print(_classes);
{One, Two} // output
and I am trying to achieve data look like this:
{'One', 'Two'}
But I can't solve it. Any suggestions why is that?
I already got it after hours of trying and trying different codes.
Set<String> _classes;
_classes = _items.map((e) => e.ClassID.toString()).toSet();
The .toString() part is the one that I am looking for.

Using \n in a string

I'm doing a Nim project with GUI and I want to show some texts which i got from my local mongoDB.
Uplaoded some of these texts like:
"something \nsomething \nsomething"
as a string. Made also a query (sorry for the format)
proc getTexts*(section : Bson) : seq[string] =
for i in 0..<len(section["texts"]):
result.add(section["texts"][i])
Then when i want to set one of these seq items as a label, or simply just echo it, looks like this:
"something \nsomething \nsomething"
not this:
"something
something
something"
Thanks in advance.
We figured out that I stored the new line char as 2 separate character
So, finally I made this short process
proc myEscape*(str: string): string =
result = str.replace("\\n", $'\n').replace("\\t", $'\t')
to replace those with only one char.
Not the nicest solution, but works.

How to cut a string from the end in UIPATH

I have this string: "C:\Procesos\rrhh\CorteDocumentos\Cortados\10001662-1_20060301_29_1_20190301.pdf" and im trying to get this part : "20190301". The problem is the lenght is not always the same. It would be:
"9001662-1_20060301_4_1_20190301".
I've tried this: item.ToString.Substring(66,8), but it doesn't work sometimes.
What can I do?.
This is a code example of what I said in my comment.
Sub Main()
Dim strFileName As String = ""
Dim di As New DirectoryInfo("C:\Users\Maniac\Desktop\test")
Dim aryFi As FileInfo() = di.GetFiles("*.pdf")
Dim fi As FileInfo
For Each fi In aryFi
Dim arrname() As String
arrname = Split(Path.GetFileNameWithoutExtension(fi.Name), "_")
strFileName = arrname(arrname.Count - 1)
Console.WriteLine(strFileName)
Next
End Sub
You could achieve this using a simple regular expressions, which has the added benefit of including pattern validation.
If you need to get exactly eight numbers from the end of file name (and after an underscore), you can use this pattern:
_(\d{8})\.pdf
And then this VB.NET line:
Regex.Match(fileName, "_(\d{8})\.pdf").Groups(1).Value
It's important to mention that Regex is by default case sensitive, so to prevent from being in a situations where "pdf" is matched and "PDF" is not, the patter can be adjusted like this:
(?i)_(\d{8})\.pdf
You can than use it directly in any expression window:
PS: You should also ensure that System.Text.RegularExpressions reference is in the Imports:
You can achieve it by this way as well :)
Path.GetFileNameWithoutExtension(Str1).Split("_"c).Last
Path.GetFileNameWithoutExtension
Returns the file name of the specified path string without the extension.
so with your String it will return to you - 10001662-1_20060301_29_1_20190301
then Split above String i.e. 10001662-1_20060301_29_1_20190301 based on _ and will return an array of string.
Last
It will return you the last element of an array returned by Split..
Regards..!!
AKsh