Город МОСКОВСКИЙ
00:08:11

How to extract integers from alphanumeric string in your Android App?

Аватар
Программирование в действии
Просмотры:
20
Дата загрузки:
05.12.2023 13:38
Длительность:
00:08:11
Категория:
Обучение

Описание

In this video it shows how one can extract or filter all the digits (integers) from any alphanumeric string/ texts.

It uses replaceAll method of the string to replace any non-numeric character with null (that is delete the non-numeric characters).

In demo it shows how from our email ID - programmerworld1990@gmail.com - the numbers 1990 is pulled out.

One can also convert the numerical string into int data type by using the below API:
Integer.valueOf(stringOfIntegers)



I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/how-to-extract-integers-from-alphanumeric-string-in-your-android-app/

However, the main Java code is copied below also for reference:


package com.programmerworld.integersfromalphanumericstring;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView textView;
private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
}

public void buttonExtractIntegersFromString(View view){
String stringInput = editText.getText().toString();
String stringIntegers = stringInput.replaceAll("[\\D]", "");
// int intString = Integer.valueOf(stringIntegers);
textView.setText(stringIntegers);
}
}

-

Рекомендуемые видео