Sahand.Setoodeh February 2016

Removing a part of a string in android

I have an app that I it has TextToSpeech feature to read lots of words in an edit text,here is my code:

package com.example.texttoSPCH;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

public class MyActivity extends Activity implements TextToSpeech.OnInitListener {
    TextToSpeech tts;
    EditText edt;
    Button btn;
    @Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
       init();
    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {

            SpeakOut();
        }

    });
}

private void init() {
    edt= (EditText) findViewById(R.id.edt);
    btn= (Button) findViewById(R.id.btn);
    tts=new TextToSpeech(this,this);
}

@Override
public void onInit(int status) {
    if (status==TextToSpeech.SUCCESS) {
        tts.setLanguage(Locale.US);
    }
    if (status==TextToSpeech.LANG_MISSING_DATA){
        Toast.makeText(getApplicationContext(),"Text To Speech is not supported",Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    tts.shutdown();
}

private void SpeakOut() {
 String text=edt.getText().toString();
    if (null==text||"".equals(text)){
        text="Please give some input";
    }
    tts.speak(text,TextToSpeech.QUEUE_FLUSH,null);
}
}

I have some words that they look like this one : ability (n.) ,or this one : dizzy (adj.), but I don't want the TTS to read the kind of that word. Is there any way to do that? like :

if(text.contains("(n.)"){
    String newtext = text-"(n.)"
}

and then tell the TTS to read newtext?

Answers


Nisar Ahmad February 2016

String stringToRemove = "(n.)";
if(text.contains(stringToRemove){
    String  newtext = text.replaceAll(stringToRemove, "");
}

Hope it works.

Post Status

Asked in February 2016
Viewed 3,210 times
Voted 14
Answered 1 times

Search




Leave an answer