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 - [email protected] - 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: [email protected]
Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...
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);
}
}
-