从字符串中删除字符

发布于 2024-11-28 04:28:10 字数 1460 浏览 4 评论 0原文

我有一个应用程序,可以在其中从 URL 解析 .txt 文件并将字符串输出给用户。我想删除字符串的前 16 个字符。我该怎么做?

编辑-我想从 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;

谢谢!

I have an app in which I parse a .txt file from a URL and spit out the string to the user. I want to remove the first 16 characters of the string. How can I do this?

EDIT- I want to remove 16 characters from the data I receive from my http call.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时浪漫 2024-12-05 04:28:10

您无法修改字符串本身,但您可以轻松创建子字符串:

line = line.substring(16);

substring 的单参数重载会获取字符串后面的整个给定起始索引。双参数重载从第一个参数指定的索引开始,到第二个参数(不包括)指定的索引结束。因此,要在“跳过”前 16 个字符后获取前三个字符,您可以使用:

line = line.substring(16, 19);

请注意,您不必分配回同一个变量 - 但您需要了解它不会影响字符串 您调用它的对象。所以:

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

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

编辑:如果您想删除整个文件的前 16 个字符,您需要:

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

如果您希望在每行基础上删除,您需要:

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

请注意,这两个字符可能都需要额外的验证 - 如果您对少于 16 个字符的字符串调用 substring(16),它将引发异常。

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. 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:

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:

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.

寂寞笑我太脆弱 2024-12-05 04:28:10

试试这个:

String newString = oldString.substring(16);

Try this:

String newString = oldString.substring(16);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文