l****n 发帖数: 55 | 1 Thanks.
I tried
s = infile.readLine()
s = infile.readLine().replaceAll("\\n+", "")
s = infile.readLine().replaceAll("[\n\r\t]+", "")
s = infile.readLine().replaceAll("\r\n|\r|\n", "")
s = infile.readLine().replaceAll("\\n", "")
It's very strange that none of them work. |
|
Q**g 发帖数: 183 | 2 看API啊。
我猜你是想要把两个backslashes换成一个是吧?应该这么写:
String foo = "\\" + "\\";
foo.replaceAll("\\\\\\\\", "\\\\");
replaceAll的第一个参数是regular expression。regular expression里
要用两个backslash '\\' 表示单个字符 '\'。然后因为你在java里头写字符串
常量也是'\\'表示一个'\'。所以第一个参数需要一共8个'\'。
第二个参数里头'\'也是有特殊含义的,所以也是。。。。
Note that backslashes (\) and dollar signs ($) in the replacement
string may cause the results to be different than if it were being
treated as a literal replacement string. Dollar signs may be
treated as references to captured subs |
|
t***a 发帖数: 416 | 3 replaceAll("\\n", "")
因为replaceAll是正则表达式,所以两次转义
"" |
|
l********r 发帖数: 140 | 4 以前用MFC的CString, 有 replace(string, string),
可是java好象没有啊.
Java String.replace is for single char replace only.
String.replaceAll is for pattern only.
How can I replace, for example, replace all the "abc" with "efg" in a given
string? (or place ", >" with "," in a given string)
Thanks a lot. |
|
c**g 发帖数: 274 | 5 Can you read document? as a developer, reading document is even
not considered as a basic skill (probably reading code is), no mention
JDK document is crystal clear. Pattern = Regular Expression.
String s = "abcadfsafd abc";
s.replaceAll("abc", "efg"); |
|
l********r 发帖数: 140 | 6 But you are wrong. The regular expression has a number of predefined
string/chars that you can NOT use.
Take a simple example:
String t1="FREE Tech Newsletters, Instant Messaging Planet Text, ");
t1=t1.toLowerCase();
String t2=t1.replaceAll(", .", ". ");
System.out.println(t2);
this will result in:
FREE Tech Newsletters. nstant Messaging Planet Text.
(the correct results one would expect --- if according to what you said,
should be the same string as t1.)
The problem is: we want to replace a sub |
|
m****r 发帖数: 6639 | 7 String foo = '\' + '\';
foo.replaceAll("\\\\", "\\");
would cause an indexOutOfBounds exception.... i don't understand. |
|
v********e 发帖数: 1985 | 8
secondly, you can check to see if it's regular expression error. pretty sure
% is a special char in regular expressions. If it is, you need to escape it.
For example, in order to replace / with \, you need to do
value.replaceAll("/","\\\\"); |
|
w***y 发帖数: 6251 | 9 如果我想把字符串中的' 换成\', 怎么处理呢?
我试了String newstr=str.replaceAll("'","\\'");
不行,'还是',没替换成\'
单独输出\用\\可以,不知道为啥后面加个'就不行了 |
|
T*****e 发帖数: 361 | 10 I guess what you need is String.replace("'", "\\'"), instead of replaceAll()
No regular expression is necessary.
You must be doing SQL statements, hehe. |
|
h*****0 发帖数: 4889 | 11 replaceAll操作是对String进行的,每次都会生成新的String
有什么办法对StringBuilder进行吗? |
|
h*****0 发帖数: 4889 | 12 或者说,需要反复的replaceAll,能不能指定每次从buffer1 replace然后复制到
buffer2? |
|
l****n 发帖数: 55 | 13 I want to put multi-lines input into single line, but str.replaceAll("\n", ""
) doesn't work
What's the right command?
Thanks |
|
a*w 发帖数: 4495 | 14 str.replaceAll("\r\n|\r|\n", "");
换行符可能不是"\n"
"" |
|
f*******n 发帖数: 12623 | 15 不要用replaceAll。用replace就行啦:
str.replace("\n", "") |
|
E***n 发帖数: 166 | 16
replace(
这个replaceAll的第一个参数是regex,如果是replaceAll("[a-c][0-9]", "a"),怎么实
现它?还是他们只要你实现字符串替换字符串就行了? |
|
D*****d 发帖数: 1307 | 17 I wrote some java code, might be ugly, but it works...
package Testing;
public class TestReplaceWithQuotes {
public static String replaceInsideQuotes(String source, char quote,
String target, String replacement){
StringBuilder regex = new StringBuilder();
StringBuilder replace = new StringBuilder();
String quoteChar = Character.isDigit(quote) || Character.
isAlphabetic(quote) ? String.valueOf(quote) : ("\" + quote);
String nonQuoteChar = "[^" + quoteCh... 阅读全帖 |
|
E*********g 发帖数: 185 | 18
:有id作为unique的key, 可以生成key-vector pair。
:
:在tree函数之前有个function给所有变量赋值,把vector里的值传给变量
:XB_TOTAL_ACTIV_MNTH_P1Q,然后tree就直接根据这些变量计算。
:
:这个code是salford软件自动生成的,所以写成这样,上千的不同名字的变量都hard
:coded
:
:1.我可以改写tree函数成接受外部array把feature的值传进来,但是每个tree只需要
5
:,6个out of hundreds.每个tree还不同的features。 我可以在textpad里一起
replace
:改,但是只可能每个tree都定义所有hundreds of变量,虽然只用5,6个,这样可以
吗?
可以
每棵树一个类是很糟糕的, 有没有可能做成一个类,不同参数
:
:有可能不改吗,维持两个function吗?一个从vector到XB_TOTAL_ACTIV_MNTH_P1Q=?的
:赋值函数,一个tree函数根据XB_TOTAL_ACTIV_MNTH_P1Q计算?spark api允许自定义... 阅读全帖 |
|
|
l**********9 发帖数: 537 | 20 请教一下,这个打印什么:
System.out.println("Java".replaceAll("\w*", "RX"));
为什么是2个RX,而不是一个RX, 谢了 |
|
l**********9 发帖数: 537 | 21 yes, it is \w*
System.out.println("Java".replaceAll("\w*", "RX")); |
|
h**********d 发帖数: 4313 | 22 都是前两个礼拜面的,下个礼拜2面,来攒个rp
AMAZON
1. Research
2. 实现Java String class的repleaceAll() method,不可以用String本身的replace(
), replaceAll()
3. 合并2个BST, 要求O(n)time
4. 2个stack -> 1个queue
GOOGLE
1. Research 因为我是做NLP的,machine learning涉及了一些,问的比较细,有点烦。
。。
2. 找一个string出现最多的字母,扩展问了很多,内存,多核CPU,多个多核CPU
machine...一开始给的hashmap的解法最后combine会有点问题,反正吭哧吭哧的答了,
最后自己觉得并没有improve多少time complexity..
祝福以下大家和自己 |
|
m****i 发帖数: 650 | 23 2. 实现Java String class的repleaceAll() method,不可以用String本身的replace(
), replaceAll()
2. 找一个string出现最多的字母,扩展问了很多,内存,多核CPU,多个多核CPU
machine...一开始给的hashmap的解法最后combine会有点问题,反正吭哧吭哧的答了,
最后自己觉得并没有improve多少time complexity..
这两个可以详细说说么 |
|
r******r 发帖数: 700 | 24 我这个方法很笨。 谁能写个 O(n) 的? 这个好像也是被 facebook 考了的。
// civic
private static boolean isCharPalindrome(String test) {
String stripped = test.toLowerCase().replaceAll("[^0-9a-zA-Z]", "");
for(int i = 0; i < stripped.length() / 2; i++) {
if(stripped.charAt(i) != stripped.charAt(stripped.length() - 1 -
i)) {
return false;
}
}
return true;
}
// ILLINOISURB
public static String longestPrefixPalindrome(String test){
Stri... 阅读全帖 |
|
g**********y 发帖数: 14569 | 25 我只能写到这样了。
public String multiply(String a, String b) {
ArrayList la = convert(a);
ArrayList lb = convert(b);
ArrayList lc = new ArrayList();
for (int i=0; i
int remainder = 0;
for (int i=0; i
for (int j=0; j
int s = lc.get(i+j) + la.get(i)*lb.get(j) + remainder;
lc.set(i+j, s%1000);... 阅读全帖 |
|
g**********y 发帖数: 14569 | 26 我只能写到这样了。
public String multiply(String a, String b) {
ArrayList la = convert(a);
ArrayList lb = convert(b);
ArrayList lc = new ArrayList();
for (int i=0; i
for (int i=0; i
int remainder = 0;
for (int j=0; j
int s = lc.get(i+j) + la.get(i)*lb.get(j) + remainder;
lc.set(i+j, s%1000);
remainder = s/1000;
}
if (remainder > 0) lc.set(i+lb.size(), remainder);
}
StringBuilder sb = new StringBui... 阅读全帖 |
|
c********t 发帖数: 5706 | 27 第一题,两个指针i,j,一个从头,一个从尾扫,但是while里面要有两个子while来跳
过标点空格,然后还要再判断越界i
问一下先用regular expression把字符串标点空格删掉(只留A-Z,a-z,0-9)如何?比如
用java replaceAll,一句就可以删掉所有标点空格。剩下就没难度了。这样的解法会
被面试官认可吗?
还有空字符串算回文吗?
多谢。 |
|
a**********0 发帖数: 422 | 28 【 以下文字转载自 Java 讨论区 】
发信人: apprentice00 (数学学徒), 信区: Java
标 题: 如何确保每次读入的字符串都是unique的
发信站: BBS 未名空间站 (Mon Jul 29 10:39:48 2013, 美东)
每次读入一个string 但是程序需要每次读入的string原先没有读入过 也就是unique的
想到了hashset 但是我的程序没有如我所愿 即使有时候读入的东西以前读到过 也照
读不误 代码如下 请帮忙指点
String result = "";
boolean flag = true;
while(true) {
for (int i = 0; i < k; i++) {
... 阅读全帖 |
|
l****o 发帖数: 315 | 29 一行就够了,s.replaceAll(" ", ""); |
|
s**********i 发帖数: 711 | 30 string.replaceAll("\n","\n"); |
|
c****r 发帖数: 185 | 31 To represent a predefined character in regular expression, just precede it
with \
Since \ is also a special character in java strings, you need another \
For example, "." should be written as "\\." |
|
l********r 发帖数: 140 | 32 that works. Thanks.
But as a curious, I just want to know how to write it if I want to repalce a
number \?
Such as I want to replace "\\\\\\\\" in a string with " ". |
|
c****r 发帖数: 185 | 33 The regex for a number of \ is \\+
So the java string should be "\\\\+"
where |
|
A**o 发帖数: 1550 | 34
'\' + '\' --> '\' + "" + '\' |
|
m****r 发帖数: 6639 | 35 i got the first "\\\\\\\\", but thought the second part was just a normal
java string. anyway, thx, it works now. |
|
l*****k 发帖数: 587 | 36 give me java.lang.NullPointerException
what went wrong??? this is in a servlet.
Thanks |
|
w*r 发帖数: 2421 | 37 debug it , your value turned out to be null somewhere before this line... |
|
c*z 发帖数: 62 | 38 % is not a special char in regular expression.
Even if it was, it throws illegal expression expression
instead of null pointer ones. |
|
l******r 发帖数: 99 | 39 what if "value is null"?
then newvalue = value.replace(....) will return a NullPointerException |
|
t**i 发帖数: 9 | 40 I ran into the following exception. I can't see '^@' in the text which I tried
to convert into a xml string. And I tried to do str = str.replaceAll('^@',
''), but it didn't help.
Do you know a way to handle this problem? Is there an ideal way that we can
handle all invalid xml characters?
I'm using xercesImpl.jar and xml-apis.jar. And I'm using xml 1.0, encoding
UTF-8.
Thanks!
java.io.IOException: The character '^@' is an invalid XML character
java.io.IOException: The character '^@' is an invali |
|
g**********y 发帖数: 14569 | 41 This is not my question. An example of my question is:
Original: "testStr==null | testStr=='ls | wc -l'"
Result: "testStr==null || testStr=='ls | wc -l'"
I'd like to see Java solution, means using String.replaceAll(), just one line
to solve it. |
|
t*****t 发帖数: 72 | 42 why not try:
if(sth) String.replaceAll();
that can make your life easier and your code more human-readable.
Anyway, I dont know Java and also the solution I posted doesnot work
coz ".." operates only line by line. while IMO, it's much easier
to figure a conditional statement instead of getting a
lookahead/lookbehind regex. You know, it could take long time
to parse your input when backtracking a non-matching string. So
I won't use regex in case I can get an alternative solution.
Just my 2 cen |
|
|
w***y 发帖数: 6251 | 44 thx! 为啥需要这么多\\\\呢? hehe |
|
g*s 发帖数: 2277 | 45 bu zhidao. just googled. because of reg expression bah.
I needed same code couple of weeks ago and just wrote a function for it. |
|
g*s 发帖数: 2277 | 46
javascript ye shi you keneng de ya. hehe. |
|
T*****e 发帖数: 361 | 47 Generating JavaScript using Java? Enn...
I did do something like populating initial values for JavaScipt variables
in JSPs. :) |
|
g**********y 发帖数: 14569 | 48 1. Java compilor need \\ to know that you want a \.
2. regexp need \\ to know that you want a \.
That's why you need \\\\ to get actually one \.
similarly, if you want a ".", you need to put "\\\." |
|
|
T*****e 发帖数: 361 | 50 I guess not if I understand it correctly.
String.replaceAll(String regex, String replacement)
The first parameter is for pattern searching, but the replacement
is simply a plain string and backreference does not work. |
|