简体   繁体   English

从字符串中删除字符

[英]Removing characters from a string

I have an app in which I parse a .txt file from a URL and spit out the string to the user. 我有一个应用程序,在其中我从URL解析.txt文件,然后将字符串吐给用户。 I want to remove the first 16 characters of the string. 我想删除字符串的前16个字符。 How can I do this? 我怎样才能做到这一点?

EDIT- I want to remove 16 characters from the data I receive from my http call. 编辑-我想从http呼叫接收的数据中删除16个字符。

public void onClick(View src) {
        switch(src.getId()) {
        case R.id.buttonRetrieveMetar:


            InputMethodManager imm = (InputMethodManager)   
getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(EditTextAirportCode.getWindowToken(), 0);


            textDisplayMetar.setText ("");


            airportcode = EditTextAirportCode.getText().toString();
            url = urlmetar + airportcode + ".TXT";

            //Added 06-27-11 METAR code
            textDisplayMetar.setText ("");

            try {
                HttpGet httpGet = new HttpGet(url);
                HttpClient httpclient = new DefaultHttpClient();
                // Execute HTTP Get Request
                HttpResponse response = httpclient.execute(httpGet);
                content = response.getEntity().getContent();
                BufferedReader r = new BufferedReader(new     
InputStreamReader(content));
                StringBuilder total = new StringBuilder();
                String line;

                while ((line = r.readLine()) != null) {
                    total.append(line);
                } 
                textDisplayMetar.append("\n" + total + "\n");
                    } catch (Exception e) {
                //handle the exception !
            }


   break;

Thanks! 谢谢!

You can't modify the string itself, but you can create a substring easily enough: 您不能修改字符串本身,但是可以很容易地创建一个子字符串:

line = line.substring(16);

The single-parameter overload of substring takes the whole of the rest of the string after the given start index. substring的单参数重载将使用给定起始索引之后的整个字符串其余部分。 The two-parameter overload starts at an index specified by the first argument, and ends at an index specified by the second argument (exclusive). 两参数重载始于第一个参数指定的索引,结束于第二个参数指定的索引(不包括)。 So to get the first three characters after "skipping" the first 16, you'd use: 因此,要在“跳过”前16个字符后获得前三个字符,请使用:

line = line.substring(16, 19);

Note that you don't have to assign back to the same variable - but you need to understand that it doesn't affect the string object that you call it on. 请注意,您不必分配回相同的变量-但您需要了解它不会影响您调用它的字符串对象 So: 所以:

String original = "hello world";
String secondPart = original.substring(6);

System.out.println(original); // Still prints hello world
System.out.println(secondPart); // Prints world

EDIT: If you want to remove the first 16 characters of the whole file, you want: 编辑:如果要删除整个文件的前16个字符,则需要:

textDisplayMetar.append("\n" + total.toString().substring(16) + "\n");

If you want that on a per-line basis, you want: 如果您希望按行进行操作,则需要:

while ((line = r.readLine()) != null) {
    total.append(line.substring(16));
}

Note that both of these may require extra validation - if you call substring(16) on a string with less than 16 characters, it will throw an exception. 请注意,这两个步骤都可能需要额外的验证-如果您对少于16个字符的字符串调用substring(16) ,则会引发异常。

尝试这个:

String newString = oldString.substring(16);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM