How to use string in DART language

In this article, i will go to explain about string in DART and methods of string.
  • 3294

String in DART

In DART string is nothing more than the array of character. String have many no of method, which allow a large no of operations on the string. You can simply define string like as:

Syntax of  DART String

type name ='value'; e.g String  strname="Welcome in DART!"; or var strname="Welcome in DART!";
  • String equality checking

    You can check two string object is same or not by (==) notation.
assert(String1==String2);
  • String concatenation

    You can concatenate string by the String.concat(String Other).
"Welcome".concat(" in Dart");
  • String start , end and contain method

    You can check a stating that  start , end or contains value is matched with  a specified string or not.
var a="Welcome in Dart";
assert(a.startsWith("Welcome"));
assert(a.endsWith("Dart"));
assert(a.contains("in"));
  • String isEmpty()

    You can check, a string is empty or not by the help of isEmpty().
var a="";
a.isEmpty();
  • String length

    You can find length of string.
var a="Welcome";
a.length;
  • Upper and Lower case method in string

    You can change string in upper and lower case.
string.toUpperCase();
string.toLowerCase();
  • Replace and  trim method in  string

    You can replace a string to other string.
a.replaceWith(from,to);
a.trim();
  • String position method

    You can find out string position.
     
e.indexOf(string,0);
  • String replaceAll()

    You can replace a string character to other character.
     
a.repalceAll("a","d");

Example of string methods

void main() {

  var a ="Welcome in Dart String";

  var c='';

  var d="WELCOME";

  var e="Welcome in Java Dart";

if(d=="WELCOME"){

  print("Compression is True");

}

if(a.startsWith("Welcome")){

  print("The given string a is start with Welcome: true");

}

if(a.endsWith("String")){

  print("The given string a is end with String: true");

}

if(a.contains("Dart")){

  print("The given string a is contains  Dart: true");

}

if(c.isEmpty()){

  print("The given string c is empty:true");

}

int len=a.length;

print("The length of string a is:${len}");

print("The upper case of string a is");

print(a.toUpperCase());

print("The lower case of string d is");

print(d.toLowerCase());

print("Repalce of Java With Google in string e is:");

e=e.replaceFirst("Java", "Google");

print(e.trim());

int pos=e.indexOf("Dart",0);

print("The index of Dart in string e is:${pos}");

print("Repalce all a from d in string a is:");

print(a.replaceAll("a", "d"));

print("The last position of E in string d is:");

print(d.lastIndexOf("E"));
}


Output:

 stringdartimage.jpg

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
© 2019 DotNetHeaven. All rights reserved.