例如我有一个字符串是1(有空格)12(有空格)17
就是1 12 17 然后我想把这个字符串 里的1替换成5 而不是12中的1 和17中的1
理想的结果应该是5 12 17 可是JAVA字符串替换之后成了 5 52 57 整个的1都替换掉了 有什么办法可以得到我想要的理想结果吗?
就是1 12 17 然后我想把这个字符串 里的1替换成5 而不是12中的1 和17中的1
理想的结果应该是5 12 17 可是JAVA字符串替换之后成了 5 52 57 整个的1都替换掉了 有什么办法可以得到我想要的理想结果吗?
public class TestReplace {
public static void main(String args[]){
String s = "1 12 17";
String str = s.substring(0,1).replace('1', '0')+ s.substring(1, s.length());
System.out.println(str);
}
}还可以用replaceFirst方法,
public String replaceFirst(String regex,
String replacement)
不过正则表达式我不懂
String str="1 12 17";
str1=str.replaceAll("1 ", "5 ");
String str2=str1;
if(str1.charAt(str1.length()-1)=='1')&&str1.charAt(str1.length()-2)==' ')
str2=str1.substring(0, str1.length-1)+"5";System.out.println(str2);
String str = "1 21 12 1 51 1";
str = str.replaceAll("(^[1]{1}\\s)|(\\s[1]{1}$)|(\\s[1]{1}\\s)"," 5 "); 5 21 12 5 51 5
*根据HMDeitel和PJDeitel合著的课本:Java How To Program. 改编
*施平安等翻译, 清华大学出版社例子改编
*第11 章 11.6 StringTokenizer394-395
*文本框中 只能输入整型数, 并用空格隔开, 否则出现异常
*/
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TokenTest extends JFrame {
private JLabel promptLabel;
private JTextField inputField;
private JTextArea outputArea;
private String result=""; // set up GUI and event handling
public TokenTest()
{
super( "Testing Class StringTokenizer" ); Container container = getContentPane();
container.setLayout( new FlowLayout() ); promptLabel = new JLabel( "Enter a sentence and press Enter" );
container.add( promptLabel ); inputField = new JTextField( 20 );
inputField.addActionListener( new ActionListener() { // anonymous inner class // handle text field event
public void actionPerformed( ActionEvent event )
{
StringTokenizer tokens =
new StringTokenizer( event.getActionCommand() ); outputArea.setText( "Number of elements: " +
tokens.countTokens() + "\nThe tokens are:\n" ); while ( tokens.hasMoreTokens() ){
String s = tokens.nextToken();
outputArea.append( s + "\n" );
int i = Integer.parseInt(s);
if (i==1){
i = 5;
s = String.valueOf(i);
}
result +=s + " ";
}
outputArea.append("最终的字符串为:" + result + "\n");
} } // end anonymous inner class ); // end call to addActionListener container.add( inputField ); outputArea = new JTextArea( 10, 20 );
outputArea.setEditable( false );
container.add( new JScrollPane( outputArea ) );
setSize( 275, 240 ); // set the window size
setVisible( true ); // show the window
} // execute application
public static void main( String args[] )
{
TokenTest application = new TokenTest();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} } // end class TokenTest
直接替换1+空格
public static void main(String[] args) {
String str = "1 21 12 1 51 1";
str = str.replaceAll("(?<!\\d)1(?!\\d)","5");
System.out.println(str);
}
}
结果 5 21 12 5 51 5
str = str.replaceAll("\\b1\\b","5");
public class Replace{
public static void main(String[] args){
String s="1 15 21";
String s1 =s.replace("1 ", "5 ");
System.out.println(s1);
}
}
public static void main(String args[]) {
String str = "1 23 12 345 2 1 -1- 1,1,1 11 111 1";
//'\b'表示单词边界
String reg = "\\b[1]\\b";
//修正值
String replacement = "5";
String s = str.replaceAll(reg, replacement); System.out.println(s);
}
}
public static void main(String args[]) {
String str = "1 23 12 345 1,11,1,2,11,1 2 1 -1- 1,1,1 11 111 1";
//'\b'表示单词边界--如果用"1 ",这样会错误地匹配 "1 23 21 2 1"中的"21";
String reg = "\\b[1]\\b";
//修正值
String replacement = "5";
String s = str.replaceAll(reg, replacement); System.out.println(s);
}
}