2

我有两个字符串 str1 和 str2。我正在尝试使用charAt将一些字母从一个字符串复制到另一个字符串。我知道我可以使用字符串复制,但我想要一些字符不是全部。

如何在Java中将子字符串从字符串复制另一个字符串?

public class MyServerSide {

    public static void main(String[] args) {
        String str1 = "Hello World!;
        String str2;
        for (int 1=0; i < str1.length(); i++){
            if (i>=3){
                str2.charAt(i) = str1.charAt(i);//Here is the problem. It gives me an error
                                                //Saying that the left argument must be a 
                                                //variable

            }//End of if statement
        }//End of for loop
    }//End of main method
}//End of class
4

9 回答 9

5

String.substring(...)如果您只想要一些字符,请使用。

编辑:

要将现有字符串与另一个字符串中的某些字符组合,您可以使用:

String anotherString = anotherString + originalString.substring(...);

要使用来自另一个字符串的某些字符创建一个新字符串,您可以使用:

String aNewString = originalString.substring(...);
于 2013-04-21T03:55:10.217 回答
4

字符串对象是不可变的,创建后无法修改。相反,您将不得不使用 StringBuilder 通过附加charAt().

于 2013-04-21T03:55:39.490 回答
2

charAt(int)方法返回指定索引处的字符,它没有设置它,使用StringBuilder类并继续附加你想要忽略其他的字符

于 2013-04-21T03:53:08.940 回答
0

您可以在两个 char 数组中转换这两个字符串并对其进行处理。在算法结束时,从第二个 char 数组重新创建 str2:

char[] ch1 = str1.toCharArray();
char[] ch2 = str2.toCharArray();

for (int i=0; i < ch1.length; i++)
    if (i>=3)
        ch2[i] = ch1[i];

str2 = new String(ch2);
于 2013-12-29T19:48:15.473 回答
0

str2.charAt(i) 函数返回一个值;它不允许在该位置设置字符 - 因此它说左侧部分应该是一个变量。

于 2013-04-21T03:56:48.407 回答
0
import java.sql.*;
import java.io.*;
import javax.sql.*;
class Ems 
{
public static void main(String args[])
{
        int ch;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =DriverManager.getConnection("jdbc:oracle:thin:@finn:1521:orcl","hr","hr");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from EmpDirc");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5)
+" "+rs.getString(6)+" "+rs.getString(7));

            do
            {
                System.out.println("\n");
                System.out.println("              ENTER EMPLOYEE DETAILS:");
                System.out.println("1.Insert Record into the Table");
                System.out.println("2.Update The Existing Record.");
                System.out.println("3.CALCULATE PRIVILAGE LEAVE");
                System.out.println("4.Display all the Records from the Table");
                System.out.println("5.Exit");
                System.out.println("Enter your choice: ");



                System.out.println("4.Exit");
                System.out.println("Enter your choice: ");

                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                ch=Integer.parseInt(br.readLine());

                  switch(ch)
                {
                    case 1:

                System.out.println("1.INSERT EMPLOYEE ID.");
                int num= Integer.parseInt(br.readLine());
                System.out.println("2.INSERT EMPLOYEE NAME");
                String ename=br.readLine();
                System.out.println("3.INSERT EMPLOYEE DESIGNATION");
                String desig=br.readLine();
                System.out.println("4.INSERT EMPLOYEE DATEOFBIRTH");
                String dob=br.readLine();
                System.out.println("5.INSERT EMPLOYEE PHONE NO OR ANY CONTACT");
                String mob= br.readLine();
                System.out.println("6.INSERT EMPLOYEE EMAIL ID");
                String email= br.readLine();
                System.out.println("7.INSERT EMPLOYEE SALARY");
                String sal=br.readLine();
                System.out.println("8.INSERT EMPLOYEE paid LEAVES");
                String pl=br.readLine();
                System.out.println("9.INSERT EMPLOYEE CASUAL LEAVES");
                String cl=br.readLine();
                System.out.println("10.INSERT EMPLOYEE FINAL SALARY");
                String fi= br.readLine();


                    String sql="insert into EmpDirc values(?,?,?,?,?,?,?,?,?,?)";
                    PreparedStatement p=con.prepareStatement(sql);
                    p.setInt(1,num);
                    p.setString(2,ename);
                    p.setString(3,desig);
                    p.setString(4,dob);
                    p.setString(5,mob);
                    p.setString(6,email);
                    p.setString(7,sal);
                    p.setString(8,pl);
                    p.setString(9,cl);
                    p.setString(10,fi);


                    p.executeUpdate();
                    System.out.println("Record Added");
                    //p.close();
                    //con.close();
                    break;


            case 2:
                    System.out.println("UPDATE EMPLOYEE id : ");
                    int emnum=Integer.parseInt(br.readLine());
            System.out.println("UPDATE EMPLOYEE DESIGNATION : ");
                    String emdesig=br.readLine();
                    System.out.println("UPDATE EMPLOYEE PHONE: ");
                    String emphn=br.readLine();
                    System.out.println("UPDATE EMPLOYEE EMAIL: ");
                    String emmail=br.readLine();
                    System.out.println("UPDATE EMPLOYEE SALARY: ");
                    String emsal=br.readLine();
                    System.out.println("UPDATE EMPLOYEE PL: ");
                    String empl=br.readLine();
                    System.out.println("UPDATE EMPLOYEE CL: ");
                    String emcl=br.readLine();
                    System.out.println("UPDATE EMPLOYEE FINAL SALARY: ");
                    String emfi=br.readLine();
                    sql="update EmpDirc set Desig=?, Phnum=? , Email=?, Salary=? , Pl=?, Cl=?, TakeHomeSal=? where Empid=?";
                    PreparedStatement  ps=con.prepareStatement(sql);

                    ps.setString(1,emdesig);
                    ps.setString(2,emphn);
                    ps.setString(3,emmail);
                    ps.setString(4,emsal);
                    ps.setString(5,empl);
                    ps.setString(6,emcl);
                    ps.setString(7,emfi);
            ps.setInt(8,emnum);
                    ps.executeUpdate();
                    System.out.println("Record Updated");
                    //p.close();
                    //con.close();
                    break;


                    case 3:
                    System.exit(0);

                    default:
                    System.out.println("Invalid Choice");
                    break;
}
}while(ch!=2);


con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
于 2015-10-21T11:35:11.150 回答
0

请参阅下面的代码,它将 首先初始化 str2

public static void main(String[] args) {
    String str1 = "Hello World!;
            String str2="";//Here initialize first
            for (int 1=0; i < str1.length(); i++){
                if (i>=3){
                   str2 = str2+str1.charAt(i);//Concatenate existing string with new String
                }//End of if statement
            }
 }
于 2020-07-25T09:12:25.077 回答
0

您可以使用 new 运算符简单地创建一个新实例。

String str1 = "abc";
String str2 = new String(str1);
于 2019-09-18T06:33:50.167 回答
-1

由于字符串是不可变的,如果您尝试仅使用上述赋值运算符进行复制,您最终会出错。相反,您可以将每个字符附加到现有字符,如下面的代码:

class Cpystring{
    public static void main(String args[])
   {
       String str1="Hello World";
       String str2="";
      for(int i=0;i<str1.length();i++)
      {
          str2+=str1.charAt(i);
       }
       System.out.println(str2);
    }

}

于 2017-01-15T11:23:26.573 回答