Android的替代新行分隔符 [英] android replace delimeter with newline
问题描述
我有分隔符中有一个字符串。我想知道一个新的生产线来代替分隔符的最佳途径。我曾与使用字符串标记的主要问题是NoSuchElementException异常的各种问题。基本上我的做法迄今为止是从数据库中检索数据每个记录一旦这项工作已经取得了我存储在一个字符串字符串问题= c.getString(1);
这里是字符串标记 StringTokenizer的ST =新的StringTokenizer(的问题,<进入>中);
通过使用while循环令牌我环路
I have a string which has delimiter in it. I want to know the best way to replace the delimiter with a new line. I have had various issues with using the String Tokenizer the main problem being NoSuchElementException. Basically my approach thus far is to retrieve data from a database Once this has been achieved I store each of the records in a string String question = c.getString(1);
Here is the string tokenizer StringTokenizer st = new StringTokenizer(question,"<ENTER>");
I loop through the tokens using a while loop
while (st.hasMoreTokens()) {
System.err.println(st.nextToken());
// quest.setText(String.valueOf(st.nextToken("<ENTER>")));
}
在$ C $工作c例如
Working example in code
String in = "What is the output of: <ENTER><ENTER>echo 6 % 4;";
in=in.substring(in.indexOf("<ENTER>")+7,in.lastIndexOf("<ENTER>"));
String[] mSplitted= in.replaceAll("<ENTER><ENTER>", "<ENTER>").split("<ENTER>");
for(int i=0;i<mSplitted.length;i++)
{
System.out.println("values: "+mSplitted[i]);
quest.setText(String.valueOf(mSplitted[i]));
}
XML code
xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/quest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="TextView" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.08"
android:text="@string/hello" />
<Button
android:id="@+id/Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
输出
推荐答案
你不能使用拆分()中的字符串的
。
can't you use split()
of String.
String in = "<ENTER>title=Java-Samples<ENTER>" +
"<ENTER>author=Emiley J<ENTER>" +
"<ENTER>publisher=java-samples.com<ENTER>" +
"<ENTER>copyright=2007<ENTER>";
in=in.substring(in.indexOf("<ENTER>")+7,in.lastIndexOf("<ENTER>"));
String[] mSplitted= in.replaceAll("<ENTER><ENTER>", "<ENTER>").split("<ENTER>");
String mFinal="";
for(int i=0;i<mSplitted.length;i++)
{
System.out.println("values: "+mSplitted[i]);
mFinal= mFinal+ mSplitted[i];
}
quest.setText(mFinal);
这篇关于Android的替代新行分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!