2

I have an app in which i have a string in which i have to count symbol and split that string accordingly and set it in textview.

code:-

int commas = 0;
String data = "Note:MRP shown , rates.The tariff plans are subject to change .Plans have been provided for information purposes only.";
for (int i=0; i < data.length(); i++) {
    if (data.charAt(i) == ".") {
        commas++;
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Niraj
  • 27
  • 1
  • 4

1 Answers1

1

Here is a one-liner you could use:

int commas = data.split("\\.").length - 1;

If you actually need to parse your text into separate sentences, you could do something similar:

String[] parts = data.split("\\.");
StringBuilder text = new StringBuilder("");
for (int i=0; i < parts.length; ++i) {
    if (i > 0) text.append("\n");
    text.append(" * " + part[i] + ".");
}

TextView yourTextView = (TextView) findViewById(R.id.tv_id);
yourTextView.setSingleLine(false);
yourTextView.setText(text.toString());

Note here that I am using HTML inside the TextView to get newlines after each sentence.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360