Extract a line from an EditText

后端 未结 3 1105
谎友^
谎友^ 2021-01-12 19:29

How can we extract a line from a multiLine EditText ?

I tried this way, but I know that is not a good practice :

String street1 = \"\";
         


        
相关标签:
3条回答
  • 2021-01-12 19:57

    Try this,

    String text=editText1.getText().toString().replace("\n", " ").trim();
    
    0 讨论(0)
  • 2021-01-12 20:01

    Try using String.split(). Code example:

    String multiLines = streetEt.getText().toString();
    String[] streets;
    String delimiter = "\n";
    
    streets = multiLines.split(delimiter);
    

    Now you have an array of streets.

    Let's say, for example, your EditText reads "1st St.\nHighway Rd.\nUniversity Ave." (or is those 3 streets separated by line breaks, instead of you actually seeing \n). Following the code example I provided you,

    • multiLines becomes "1st St.\nHighway Rd.\nUniversity Ave."
    • streets = multiLines.split(delimiter); fills the array streets with the street names, i.e.

      • streets[0] = "1st St."
      • streets[1] = "Highway Rd."
      • streets[2] = "University Ave."
    0 讨论(0)
  • 2021-01-12 20:04

    Try this:

    String text = streetEt.getText();
    
    String firstLine = text.substring(0,text.indexOf("\n"));
    
    0 讨论(0)
提交回复
热议问题