java 字符串连接为字符数组(无库方法)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17265781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:30:51  来源:igfitidea点击:

String concatenation as character array (without library method)

java

提问by Devendra Singh

I want to store the value of 2 strings str1, str2 respectively into 3rd string strContainer (without using library method).

我想将 2 个字符串 str1、str2 的值分别存储到第 3 个字符串 strContainer 中(不使用库方法)。

My Algorithm is:
1. convert str1 and str2 into character array charArray1 and charArray2 respectively.
2. Count the sum of the length of both character array in counter variable (sum of the length charArray and charArray2)
3. Sum of the both char Arrays (as counter) is equivalent to a new char Array charContainer.
4. Iterate a loop as below
charContainer[ith index] += charArray1[ith index];
and
charContainer[ith index] += charArray2[ith index];
5. Convert charContainer into string and display as strContainer.

我的算法是:
1. 将 str1 和 str2 分别转换为字符数组 charArray1 和 charArray2。
2.计算计数器变量中两个字符数组的长度之和(长度charArray和charArray2的
总和)3.两个char数组之和(作为计数器)相当于一个新的char数组charContainer。
4. 循环如下
charContainer[ith index] += charArray1[ith index];

charContainer[第 i 个索引] += charArray2[第 i 个索引];
5. 将charContainer 转换为字符串并显示为strContainer。



My code so far:

到目前为止我的代码:

public class StringConcat{
  int counter; // counting the length of char arrays
  String str1 = "FirstString";
  String str2 = "SecondString";

  //for counting the length of both char arrays
  public int countingLength(String str){
    char[] strToCharArray = str.toCharArray();
    for(char temp : strToCharArray){
     counter++;
    }    
  }

 //converting string into char array
 char[] charArray1 = str1.tocharArray();
 char[] charArray2 = str1.tocharArray();

 //stores both char array   
 char[] charContainer=new char[counter];//how to take counter as an index value here

  //for storing charArray1 into charContainer
  for(int i=0; i<charContainer.length; i++) {

      if(charArray1 != null){
        charContainer[i] +=  charArray1[i];
      } else 
        return charArray2; 
  }  

  //for storing charArray2 into charContainer
  for(int i=0; i<charContainer.length; i++) {

      if(charArray2 != null){
        charContainer[i] +=  charArray1[i];
      } else 
        return charArray1; 
  }  

  //converting charContainer char array into string strContainer.
  String strContainer = new String(charContainer);
  //alternative : String strContainer = String.valueOf(charContainer); 

 public static void main(String args[]){

   /*Here i can call (As i'm not sure) 
     StringConcat obj1 = new StringConcat();
      obj1.countingLength(str1);

     StringConcat obj2 = new StringConcat();
      obj2.countingLength(str2);        
  */
   System.out.println("String Container : " +strContainer);  
 }  

}//end of the class

Issues:
How to call countingLength() method for both strings str1 and str2 ?
How to assign as an index value of charContainer as counter (sum of the both char arrays) ?

问题:
如何为字符串 str1 和 str2 调用 countingLength() 方法?
如何将 charContainer 的索引值分配为计数器(两个 char 数组的总和)?

回答by Marco Martinelli

How to call StringLengthCounter() method? I can't see any method with that name.. I'm sorry but that is not the problem here, the problem is that this is not even valid code.

如何调用 StringLengthCounter() 方法?我看不到任何具有该名称的方法.. 对不起,但这不是这里的问题,问题是这甚至不是有效代码。

I don't mean to be harsh but there are sintax error all around and the program logic is wrong in many ways.

我并不是要苛刻,但到处都是语法错误,程序逻辑在很多方面都是错误的。

Please take a look at the following code and try to figure out how it works, I think it does what you want. If something isn't clear just ask.

请查看以下代码并尝试弄清楚它是如何工作的,我认为它可以满足您的需求。如果有什么不清楚的就问吧。

public class StringConcat{
    public static String strcat(String str1, String str2){
        //converting string into char array
        char[] charArray1 = str1.toCharArray();
        char[] charArray2 = str2.toCharArray();

        int counter=charArray1.length+charArray2.length;

        //stores both char array   
        char[] charContainer=new char[counter];

        //for storing charArray1 into charContainer
        int i=0;
        for(; i<charArray1.length; i++) {
            charContainer[i]=charArray1[i];
        }
        //for storing charArray2 into charContainer
        for(int j=0; i<counter; j++,i++) {
            charContainer[i]=charArray2[j];
        }
        //converting charContainer char array into string
        return new String(charContainer);
    }
    public static void main(String args[]){
        String str1 = "FirstString";
        String str2 = "SecondString";
        String strContainer = strcat(str1,str2);
        System.out.println("String Container : " +strContainer);  
    }
}//end of the class

回答by Manish Kumar

import java.io.*;
class Concatenation
{
    public static void main(String []args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int i=0;
        String s1,s2;
        System.out.print("Enter the first string:");
        s1=br.readLine();
        System.out.print("Enter the Second string:");
        s2=br.readLine();
        char s3[]=new char[s1.length()+s2.length()];
        for(;i<s1.length();i++)
            s3[i]=s1.charAt(i);
        for(int j=0;j<s2.length();j++)
            s3[i++]=s2.charAt(j);
        System.out.println("Result:"+new String(s3));
    }
}

回答by sravani royal

public class Conc {
    String s1="java",s2="programming",s3="";
    int l=s1.length(),m=s2.length(),i,j;
    public String conca(String s1,String s2){
        for(i=0;i<=l-1;i++){
            s3+=s1.charAt(i);
        }
        for(j=0;j<=m-1;j++){
            s3+=s2.charAt(j);
        }
        System.out.println(s3);
return s3;
    }
    public static void main(String[] args) {

        Conc obj1=new Conc();
        obj1.conca("java","programming");

}
}

回答by Naseer

public class StringConcatination {

    public static String concate(String s1,String s2){
        String s3="";
        for(int i=0;i<s1.length();i++){
            s3+=s1.charAt(i);
        }
        for(int j=0;j<s2.length();j++){
            s3+=s2.charAt(j);
        }

        return s3;
    }

    public static void main(String[] args) {
        System.out.println(concate("java","programming"));
    }    
}

回答by Arivazhagan L

public static void concat(String a, String b) {
    /**
     * String result = a + b;
     */

    /**
     * Logic goes here.
     * 
     * Need to iterate through chars
     * 
     * Need to get total length
     */
    int totalLength = a.length();
    totalLength += b.length();
    char[] result = new char[totalLength];
    char[] arrayFromA = a.toCharArray();
    char[] arrayFromB = b.toCharArray();
    int count = 0;
    System.out.println("Total Length of String: "+ totalLength);
    for (int i = 0; i < arrayFromA.length; i++) {
        result[i] = arrayFromA[i];
        count++;
    }
    for (int j = 0; j < arrayFromB.length; j++) {
        result[count] = arrayFromB[j];
        count++;
    }
    System.out.println(new String(result));
}