flutter - if then else - variable made local only - flutter

As you can see in the code below, I want to check if a string has a ? in his name.
If yes, I remove it.
My problem is that the variable 'nameOfFileOnlyCleaned' stay local and is empty after the if else.
Thank you for your help.
String nameOfFile = list_attachments_Of_Reference[j].toString().split('/').last;
if (nameOfFile.contains('?')) { //Removes everything after first '?'
String nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
String nameOfFileOnlyCleaned = nameOfFile;
}
//Here my variable 'nameOfFileOnlyCleaned' is empty.
This is a problem because the value should be used later
in the code. Do you know why I have this issue please?
Many thanks.
String extensionFile = nameOfFileOnlyCleaned.split('.').last;
String url_Of_File = list_attachments_Of_Reference[j].toString();

you should define your variable before if/else statement as follows:
String nameOfFileOnlyCleaned = "";
if (nameOfFile.contains('?')) { //Removes everything after first '?'
nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
nameOfFileOnlyCleaned = nameOfFile;
}
for example:
String nameOfFile = "test?test";
String nameOfFileOnlyCleaned;
if (nameOfFile.contains('?')) { //Removes everything after first '?'
nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
nameOfFileOnlyCleaned = nameOfFile;
}
print(nameOfFileOnlyCleaned);
it returns: test as a result.

Related

Get file extension from file name string

I was trying to get file extension with dart code as this,
var url = downloadUrl.toString();
String fileExtension = '';
String fileName = path.split("\/").last;
bool fileExt = false;
for (var characterUrl = 0;
characterUrl < fileName.length;
characterUrl++) {
if (String.fromCharCode(fileName.codeUnitAt(characterUrl)) == '.' && !fileExt) {
fileExt = true;
}
if (fileExt) {
fileExtension += String.fromCharCode(fileName.codeUnitAt(characterUrl));
}
}
When having a file named as:
eg: Screenshot_2021-06-10_co.com.app.jpg
It returns me:
".com.app.jpg"
But I would like just to get the .jpg
In other file names it works perfectly.
You're overthinking this solution here. Just use methods already made for you in the String class. Since you already have the file name, you can find the last instance of the . character with the preexisting lastIndexOf method and get the substring with that index:
String fileName = 'Screenshot_2021-06-10_co.com.app.jpg';
String fileExt = fileName.substring(fileName.lastIndexOf('.'));

Scoping problem when trying to assign vector of strings in DOORS DXL

I want to be able to send a selector value to a function, and have the function create a string array of values for use in subsequent operations. The problem I'm running into is that the DXL rules don't "see" a declared variable inside an "if {} " block, even when I've guaranteed that in all cases my string array would get initialized. Example:
string tryme( int thechoice){
string outit
if (thechoice == 1){
outit = "you chose one"
}
else if (thechoice ==2){
outit = "you chose two"
}
else { outit = "bad choice"}
// do a bunch of stuff with "outit" values
return outit
}
// that works, but this doesn't
string trymore( int thechoice){
if (thechoice == 1){
string outit[] = {"you chose one","and one"}
}
else if (thechoice ==2){
string outit[] = {"you chose two","and two", "and three"}
}
else { string outit = "bad choice"}
// do a bunch of stuff with "outit" values
return outit
}
I could use dynamic arrays, thus allowing me to declare the array prior to the "if{}", but then I'm forced to write loops inside each case, using put to load the array.
I know DXL has a limited capability, but if anyone knows a better approach please let me know.
additional constraint
In the end, I want to be able to call the function repeatedly from a loop, changing "thechoice" each time. That is why I can't declare the string array in the parent script, because once declared ( string outit[] = {'a','b'} ) , DXL cannot delete or resize the array.
It's easier than that, tho' a bit counterintuitive . I need to declare a string vector with out assigning anything, then generate a temporary string vector, then set my desired variable equal to the temp. Like this:
string trymore( int thechoice){
string outit[]
string whatdone
if (thechoice == 1){
string foo[] = {"you chose one","and one"}
whatdone = "did one"
outit = foo
}
else if (thechoice ==2){
string foo[] = {"you chose two","and two", "and three"}
whatdone = "did two"
outit = foo
}
else {
string foo[] = "bad choice"
whatdone = "nogood"
outit = foo
}
print "outit " outit[0] "\n"
// do a bunch of stuff with "outit" values
return whatdone
}
DXL will not allow you to assign the values to outit , crying "length mismatch", but will allow you to set outit equal to the fully defined foo

Iterate over part of String in Swift

Why in the world are Swift String operations so complex and tiresome to work with?
I have to iterate over a String in reverse but ignoring the first char. Now this could be done like following:
var firstTime = true
for i in textBefore.characters.reversed() {
if firstTime {
firstTime = false
} else {
if String(i).personalFunction() {
// something
} else {
// something else
}
}
}
But really I just want to do something like:
textBefore = textBefore.characters.reversed()
for i in 1...textBefore.characters.count {
if textBefore.get(i).personalFunction() {
// something
} else {
// something else
}
}
So why can't we get index as int. And why is textBefore.characters.reversed() not a String or simply have String have a reverse function. All these issues just makes it so frustrating to work with Strings in Swift and makes us do stupid stuff as converting a String to an array of chars :S or stuff like my proposed solution above... Also we can't make for loops in the old fashion... I simply need some Swift guru to point my brain in the right direction for this stuff.
string.characters is a collection of characters.
Use reversed() to access the elements in reverse order, anddropFirst() to skip the initial element of the reversed collection:
let string = "a🇨🇷b😈"
for ch in string.characters.reversed().dropFirst() {
print(ch)
// `ch` is a Character. Use `String(ch)` if you need a String.
}
Output:
b
🇨🇷
a
You can do something like your second one. After you enter the for, you can just get the index directly from the string. In Swift, a string is just an array of characters.
textBefore = String(textBefore.characters.reversed())
for i in 1...textBefore.characters.count {
if textBefore[i].personalFunction() {
// something
} else {
// something else
}
}

Javascript create a function that returns a boolean value based on certain parameters

Thanks for taking the time to look at my problem. What I'm trying to do is create a javascript function that tests whether a sting is a particular length and also whether each element of that string can be found in another string. The function then needs to return a boolean value of either true or false depending on whether the string is valid.
Here's what I have:
N_ALPHA = 6;
N_CHOICES = 4;
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var alphabet = ALPHABET.substring(0, N_ALPHA);
function isValidGuess(inStr)
{ var valid;
var Str = inStr;
for (i=0; i<Str.length; i++)
{ if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES.length)
{ valid = true;
}
else
{ valid = false;
}
}
return valid;
}
This code is not working at all. It only returns false every time. Any help you could provide would be greatly appreciated. Thank you.
N_CHOICES.length return undefined, because variable N_CHOICES is number.
you have to change your condition to
if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES)

How to compare self.title to a string in Objective C?

What am I doing wrong in the below code? My if statement is missing something:
if ([self.title] = "Upcoming Events") {
} else {
}
Correct would be:
if( [self.title isEqualToString:#"Upcoming Events"] )
self.title is a pointer, you can only compare it to other pointers using == not their values.
if ([self.title isEqualToString:#"Upcoming Events"]) {
NSLog(#"True");
} else {
NSLog(#"False");
}
You just have to write like this:
if([self.title isEqualToString:#"Upcoming Events"])
{
}
else
{
}
also .... in a if you should use "==" instead of "=". When there is "==" it is checking if they are equal while if there is "=" it gives the first one the value of the second.