我知道str.replace(/x/g, "y")替换字符串中的所有x但我想这样做
function name(str,replaceWhat,replaceTo){
str.replace(/replaceWhat/g,replaceTo);
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在第一个参数中使用变量?
这是我的isPrime方法:
private static boolean isPrime(int num) {
if (num % 2 == 0) return false;
for (int i = 3; i * i < num; i += 2)
if (num % i == 0) return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
我把isPrime(9)它放回去了true.这个方法有什么问题?
我有一个非常长的字符串,我从书中得到.我使用setText()方法在JTextArea中显示它.它冻结了UI并花费了大量时间.我该如何解决这个问题?
这是SSCCE:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class SSCCE extends JFrame {
JTextArea textArea;
public SSCCE() {
String text = buildLongString(400000);
textArea = new JTextArea();
textArea.setText(text);
textArea.setLineWrap(true);
add(new JScrollPane(textArea));
setSize(400, 350);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private String buildLongString(int length) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; i++) {
builder.append("x");
}
return builder.toString();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SSCCE(); …Run Code Online (Sandbox Code Playgroud) 这是我的代码.
String x = "1+√10";
x = x.replace(".*", "x");
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)
这应该返回,"x"但它正在返回"1+√10".为什么这不起作用?
if(document.URL!="location.php?img_url="+img_url){
window.location.href = "location.php?img_url="+img_url;
}
Run Code Online (Sandbox Code Playgroud)
这会不断重新加载页面.我检查网址,看它是否发生变化,但不是.
这段代码有什么问题?
addKeyListener(new KeyHandler());
private class KeyHandler extends KeyAdapter {
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int key = e.getKeyCode();
System.out.println("test");
if(key==KeyEvent.VK_SPACE || key==KeyEvent.VK_ENTER || key==KeyEvent.VK_P) {
paused = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这应该在控制台中打印测试,但它没有.我究竟做错了什么?