Add Your Heading Text Here

A data type String is a sequence of UTF-16 code units.

Data type String is the representation of sequence of characters.

Data type String is used for storing values other than numbers like words, sentences etc. in variable.

To declare any varible as String, “String” keyword is used.

You can represent String with words or sententences or quotes in between the single or double quotes.

For ex :

String variableName=”Toastguyz”;
String variableName=’Toastguyz’;

But, dart also provide facility for declaring String variable values in between n number of starting quotes and n number of ending quotes.

For ex :

String variableName=””ToastGuyz””;

You cannot use String variables starting with 4 quotes and ending with 10 quotes for storing the string values.

For ex :

String variableName=””ToastGuyz”””””’; // will generate the compile time error.

We can use string for storing different kind of values like words, sentences or data information etc.

For ex :

void main(){
	String name="ToastGuyz";
	String hobbies="I Love Coding";
	String sarcasm="Are you Smart?";

	print(name);
}

String Manipulation

You can add/append one String value to another String value to generate a new String is termed as Concatenation(Interpolation).

To concatenate one String to another +(plus) operator is used.

For ex :

void main(){
	String firstName="Bill";
	String lastName="Gates";

	print("Name = " + firstName + lastName);
}

Therefore, string manipulation can be helpful to format the String data in appropriate manner as well as for individual needs.

We can use ‘$’ identifier for dynamically change the value of the expression within Strings.

For ex :

void main()
{
		int value=10;
		print("My Favourite Number is $value");
}

Output :

My Favourite Number is 10

You can also manipulate the expression within Strings using ‘${expression}’

So using ‘${}’ identifier we can also manipulate the expression in very neat and handy manner.

For ex :

void main()
{
	int a=10;
  	int b=20;
	print("Total of the Sum is ${a+b} ");
}

Output :

Total of the Sum is 30

Users can also manipulate the 2 string variables in between the expressions like,

For ex :

void main()
{
    		String a='Strongest';
    		String b='Avenger';
    		print("Hulk is the ${a+b}.");
}

Output :

Hulk is the StrongestAvenger.

Manipulating strings in different ways using your logic is fun.

String Common Properties

For ex :

void main()
{
		String a='Strongest';
  
  		print(a.length);
  		print(a.codeUnits);
  		print(a.runes);
  		print(a.runtimeType);
  		print(a.isEmpty);
  		print(a.isNotEmpty);
}

Output :

9
[83, 116, 114, 111, 110, 103, 101, 115, 116]
(83, 116, 114, 111, 110, 103, 101, 115, 116)
String
false
true

String Common Methods

For ex :

void main()
{
	String a='Strongest';
  	String b="Avenger";
  	String c="ABCDEF";
  	String d="abcdef";
        String e="  ab cdefghi ";
  	String f="def";
  	String g="dek";
  
  	print(a.substring(2,5));
  	print(a.codeUnitAt(5));
  	print(a.startsWith("Str"));
  	print(a.replaceAll("ron","toytoy"));
  	print(a.split("n"));
  	print(a.contains("ron"));
  	print(a.indexOf("r"));
  	print(c.toLowerCase());
  	print(d.toUpperCase());
  	print(e.trim());
        print(a.endsWith("est"));
  	print(f.compareTo(g));
  	print(a.toString());
}

Output :

ron
103
true
Sttoytoygest
[Stro, gest]
true
2
abcdef
ABCDEF
ab cdefghi
true
-1
Strongest

In this way, different built-in properties and methods of String data type makes it easy and efficient for performing some handy work.

So practice the String data types with more and more programming definitions to get grip on it.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons