从字符串中删除字符
我有一个应用程序,可以在其中从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法修改字符串本身,但您可以轻松创建子字符串:
substring
的单参数重载会获取字符串后面的整个给定起始索引。双参数重载从第一个参数指定的索引开始,到第二个参数(不包括)指定的索引结束。因此,要在“跳过”前 16 个字符后获取前三个字符,您可以使用:请注意,您不必分配回同一个变量 - 但您需要了解它不会影响字符串 您调用它的对象。所以:
编辑:如果您想删除整个文件的前 16 个字符,您需要:
如果您希望在每行基础上删除,您需要:
请注意,这两个字符可能都需要额外的验证 - 如果您对少于 16 个字符的字符串调用
substring(16)
,它将引发异常。You can't modify the string itself, but you can create a substring easily enough:
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: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:
EDIT: If you want to remove the first 16 characters of the whole file, you want:
If you want that on a per-line basis, you want:
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.试试这个:
Try this: