如何将引号赋值给字符串
有如何将包括双引号的字符串赋值给字符变量
如:hello "tomcat".
还有一个问题:
如何确定字符串中的中文字符串位置:
如:黑龙江电视台<div align="center">
急!谢谢!!
问题点数:0、回复次数:7Top
1 楼LoveRose(旺旺)回复于 2003-11-04 16:02:24 得分 0
String str = "\"tomcat\"";
System.out.println(str);Top
2 楼lynx1111(任我行:一个PLMM看着就兴奋的男人)回复于 2003-11-04 16:05:48 得分 0
String str="hello \"tomcat\". ";
System.out.print(str);Top
3 楼lcz022(阿五)回复于 2003-11-04 16:33:57 得分 0
String str="hello \"tomcat\"";
out.println(str);
String s = "黑龙江电视台<div align=\"center\"> "
int location = s.indexOf("黑龙江电视台");
Top
4 楼hyhu(先飞笨鸟)回复于 2003-11-04 16:41:24 得分 0
加转义字符\,如"\"abc\""就是你想得效果!Top
5 楼soke(倚剑横行)回复于 2003-11-04 16:42:16 得分 0
上面的朋友报错:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1Top
6 楼soke(倚剑横行)回复于 2003-11-04 16:44:25 得分 0
各谢各位朋友:
利用转义字符\可以实现,可中文问题没有解决,
请各位多费心!!Top
7 楼soke(倚剑横行)回复于 2003-11-05 08:19:07 得分 0
程序代码如下:
请各位朋友帮忙
import java.io.*;
import java.net.*;
/**
* This simple program uses the URL class and its openStream() method to
* download the contents of a URL and copy them to a file or to the console.
**/
public class GetURL {
public static void main(String[] args) {
InputStream in = null;
OutputStream out = null;
String str=null,s=null;
int i=0,location=0;
try {
// Check the arguments
if ((args.length != 1)&& (args.length != 2))
throw new IllegalArgumentException("Wrong number of args");
// Set up the streams
URL url = new URL(args[0]); // Create the URL
in = url.openStream(); // Open a stream to it
if (args.length == 2) // Get an appropriate output stream
out = new FileOutputStream(args[1]);
else out = System.out;
// Now copy bytes from the URL to the output stream
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = in.read(buffer)) != -1)
str=new String(buffer,0,bytes_read,"gb2312");
s="中央电视台1";
//s=new String(s.getBytes("8859_1"));
//System.out.println(s);
s=new String(s.getBytes("gb2312"));
//System.out.println(s);
if (str.indexOf(s)!=-1){
str=new String(buffer,0,i+30);
bytes_read=str.lastIndexOf("a",i+30);
System.out.println(i);
System.out.println(str);
str=str.substring(i,bytes_read);
System.out.println(bytes_read);
System.out.println(str);
str="\"tomcat\"中央电视台1";
System.out.println(str);
i=str.indexOf("中央电视台1");
System.out.println(i);
}
//out.write(buffer, 0, bytes_read);
}
// On exceptions, print error message and usage message.
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java GetURL <URL> [<filename>]");
}
finally { // Always close the streams, no matter what.
try { in.close(); out.close(); } catch (Exception e) {}
}
}
}
运行:
java GetURL http://www.daqing.gov.cn/spdb/tv/cctv1.htm
Top




