Let’s work with various strings’ operations…

Concatenating two Strings

You can concatenate strings using adjacent string literals or the + operator.

Code 1:

void main(){
  var a=’dart’;
  var b=’flutter’;
  print(a+b);
}
output

Let’s see other examples…

Code 2:

void main(){
  String fname=”shimanta”;
  String lname=”das”;
  // concatinating using + operator
  String full=fname+lname;
  print(full);
  print(full.runtimeType);
}
output

code 3:

void main(){
  late var a;
  late var b;
 
  // initializing values
  a=’linux’;
  b=’debian’;
  // ‘ ‘ creates space
  String c=a+’ ‘+b;
  print(c);
}
output

RAW String

In a raw string, special characters not get a special treatment. You can create a “raw” string by prefixing it with r.

Code 4:

void main(){
 
  // without RAW string
  print(‘non-raw string -> ‘);
  var nonR=’this is mushoor gulati \n he is no.1′;
  print(nonR);
  // with RAW string
  print(‘raw string -> ‘);
  var withR=r’this is mushoor gulati \n he is no.1′;
  print(withR);
}
output

String Contains

We can check whether a substring contains inside a large string or not using .contains() methods. It returns a Boolean value (true/false) as output.

Code 5:

void main(){
  var os=’there 3 type of OS -> windows,linux[ubuntu,android],max’;
  print(os.contains(‘linux’));
}
output

true

You can perform control statements such as if_else according to this… let’s see below…

Code 6:

import ‘dart:io’;
 
void main(){
  late String? find;
  print(‘Enter String to find: ‘);
  // getting and setting user input into String variable->’find’
  find=stdin.readLineSync();
 
  String name=”dipsayak,aditi,shravani,ramesh,shimanta,debo,salman,roni,nishant,gopal,bidyut”;
  if (find!=null) {
  if (name.contains(find)) {
    print(‘String found!’);
  }
  else{
    print(‘String not found!’);
  }
  }
}
output

Access String Characters

There are many way to access string characters – using .substring() method and using index.

Substring generally print string’s characters from an index. let’s see operation using this below…

Code 7:

void main(){
  var os=”windows”;
  // .substring(start)
  print(os.substring(3));
  print(os.substring(5));
  print(os.substring(0));
}
output

code 8:

void main(){
  String os=’windowsLinuxMacos’;
  print(‘index 0 element -> ‘);
  print(os[0]);
  print(‘index 7 element -> ‘);
  print(os[7]);
}
output

String length

For getting String’s length we use .length property. Basically number of character inside also known as length of that string. Let’s see below code…

Code 9:

void main(){
  late var str;
 
  str=”windows”;
  print(‘string length is’);
  print(str.length);
  str=”Linux”;
  print(‘string length is’);
  print(str.length);
  str=”kali linux”;
  print(‘string length is’);
  print(str.length);
}
output

String start with? End with? Index of substring?

Let’s see below code to understand this example…

.startwith() method return boolean value(true/false) if it found starting string pattern.
.endwith() method return boolean value(true/false) if it found end string pattern.
.indexof() method finds index of substring.

Code 10:

void main(){
  String name=”linux is better for security than windows”;
  print(name.startsWith(‘lin’));
  print(name.startsWith(‘ows’));
}
output

code 11:

void main()
{
  var name=”windows better for GUI than Linux”;
  print(name.endsWith(‘nux’));
  print(name.endsWith(‘win’));
}
output

code 12:

void main(){
  String name=”Linux Mint”;
  // prints index of character ‘L’
  print(name.indexOf(‘L’));
  // prints index of character ‘M’
  print(name.indexOf(‘M’));
}
output

String Characters Equality Check

We can simply compare two strings which equal to each other or not using == operator.

Code 13:

void main(){
  String name1=”sanjoy”;
  String name2=”sanjoy”;
  if (name1==name2) {
    print(‘String matched!’);
  }
}
output

code 14:

import ‘dart:io’;
 
void main(){
  String? mob1,mob2;
 
  print(‘Enter first string: ‘);
  mob1=stdin.readLineSync();
  print(‘Enter second string: ‘);
  mob2=stdin.readLineSync();
  print(‘String matches ? ‘);
  print(mob1==mob2);
}
output

String case conversion

Now let’s see how to convert uppercase to lowercase and reverse operation also.

Code 15:

void main(){
  String str=”coding”;
  print(str);
  // toUpperCase() method uses to convert lowercase String to Uppercase
  str=str.toUpperCase();
  print(str);
}
output

code 16:

void main(){
  String str=”LINUX”;
  print(str);
  // toLowerCase() uses to convert uppercase String to lowercase
  str=str.toLowerCase();
  print(str);
}
output

Trimming and replace strings

Remove all leading and trailing white space with trim().

Code 17:

void main(){
  String name=” microcodes “;
  print(name);
  // trim from both side
  name.trim();
   print(name);
 // trim from left side
 name.trimLeft();
 print(name);
 // trim from right side
 name.trimRight();
 print(name);
}
output

Strings are immutable objects, which mean you can create them but you can’t change them. If you look closely at the String API reference in Dart, you’ll notice that none of the methods actually changes the state of a String. For example, the method replaceAll() returns a new String without changing the original String.

replaceAll(‘pre-string’, ‘modify-string’)

code 18:

void main(){
  String name=”windows is better for server! windows is much secure”;
  print(‘before changing string -> ‘);
  print(name);
  name=name.replaceAll(‘windows’, ‘Linux’);
  print(‘after changing string -> ‘);
  print(name);
}
output

String to hashcode

Let’s understand through below code…

Code 19:

void main(){
  // .hashcode converts string to it’s hashcode
  String password=”shi123″;
  print(password);
  print(password.hashCode);
 
  password=”microcodes”;
  print(password);
  print(password.hashCode);
}
output