In this article, we’ll learn how to implement Flutter string concatenation by using multiple easy Flutter code examples.
After reading this post, you’ll be able to easily implement concatenation of strings in your own Flutter code as well.
So let’s get right into its practical implementation.
What is Flutter String Concatenation?
It specifies the process of joining two or more strings. We’ll look at 4 ways though which we can concatenate/join Flutter strings.
So let’s start implementing it using practical Flutter code examples.
Implementing Flutter String Concatenation (4 Easy Ways)
These 4 ways are listed below:
- Using direct String Literals
- Using join() Method
- Using + Operator
- Using String Interpolation
Using Direct String Literals
In this method, we’ll be writing the strings directly. See below code:
print('My'' name'' is'' Zeeshan');
Output
My name is Zeeshan
You can see that we can concatenate Flutter strings using direct usage of string literals. Just give some space after each string if you want them to have some distance from each other.
You can also assign it to a variable. See below code:
String val='My'' name'' is'' Zeeshan'; print(val); // My name is Zeeshan
Using join() Method
We can use join method to convert list of strings into just one string. See below example:
List listItems=['My','name','is','Zeeshan']; String val= listItems.join(); print(val);
Output
MynameisZeeshan
You can pass integer or double as well and this join method will successfully concatenate it.
If you want to specify a space or some character between the string items then specify it inside join method. See below code:
List listItems=['My','name','is','Zeeshan']; print(listItems.join(' ')); // My name is Zeeshan print(listItems.join('-')); // My-name-is-Zeeshan
Using + Operator
We can also use + operator to perform Flutter string concatenation. See below code:
String a='This '; String b='color '; String c='is Blue'; String value= a+b+c; // concatenating all strings using + operator print(a+b+c); // This color is Blue print(value); // This color is Blue
Output
This color is Blue This color is Blue
So this is how we can concatenate strings using + operator.
Using String Interpolation
Finally, the last method is string interpolation. We can also use this way to concatenate strings in Flutter. See below example:
String a='This '; String bb='color '; String c='is Green'; String value= '$a $bb $c'; // the dollar sign will specify that take this character/characters as a variable and get its value. print('$a $bb $c'); // This color is Green print(value); // This color is Green
Output
This color is Green This color is Green
We can either print it directly or we can store it in a new variable and then use it wherever we want.
So this is how we can properly implement Flutter string concatenation in our own Flutter code. Hope you’ve learned alot from it.
Conclusion
To conclude, hope you now have a proper practical knowledge of how to perform Flutter string concatenation using multiple methods. I’ll be looking forward at your feedback on this post.
I’d also highly recommend you to visit my other articles as well. Thank you for reading this post.