Home > other >  Dart find the capital word in a given String
Dart find the capital word in a given String

Time:01-20

How can I write a code that finds capital words in a given string in dart

for example

String name ='deryaKimlonLeo'
//output='KL'

CodePudding user response:

A basic way of doing this is like

void main() {
  String name = 'deryaKimlonLeo';

  String result = '';
  for (int i = 0; i < name.length; i  ) {
    if (name.codeUnitAt(i) <= 90 && name.codeUnitAt(i) >= 65) {
      result  = name[i];
    }
  }

  print(result);
}

More about ASCII and codeUnitAt.

CodePudding user response:

Hmmm try this using pattern if you only want to get only the big letter then try this

String name ='deryaKimlonLeo';
print(name.replaceAll(RegExp(r'[a-z]'),""));

//Output you wanted will be 
// KL

try this on dart pad it works

CodePudding user response:

sample code:

void main() {
  String test = "SDFSDdsfdDFDS";
  for (int i = 0; i < test.length; i  ) {
    if (test[i].compareTo('A') >= 0  && test[i].compareTo('Z') <= 0)
      print(test[i]);
  }
}
  •  Tags:  
  • Related