开发者

String Manipulation in java

开发者 https://www.devze.com 2023-02-14 04:51 出处:网络
I have one array of strings. I want to get each 开发者_运维百科of string, divide it in to 3 parts (number-string-number), and put each part in another array. At last I want to have 3 arrays which two

I have one array of strings. I want to get each 开发者_运维百科of string, divide it in to 3 parts (number-string-number), and put each part in another array. At last I want to have 3 arrays which two of them store numbers and one of them stores strings. The number of spaces between numbers and strings are not fixed.

the format of the strings in the first array is:

-2.2052 dalam -2.7300

-3.0511 dan akan -0.1116

It will be great if you help me with a sample code.


Here's the algorithm you could implement :

  1. Create your 3 output arrays. They should all have the same length as the original string array
  2. iterate through your original array.
  3. for each string, find the index of the first space character and the index of the last space character. (look into the javadoc of the String class for methods doing that)
  4. extract the substring before the first space, the substring between the first and last space, and the substring after the last space. The javadoc should help you.
  5. Convert the first and third substring into an int (see the javadoc for Double for how to do it)
  6. store the doubles and the string into the ouput arrays.


You can use indexOf and lastIndexOf to achieve this. Try following:

        String arrayWithStringAndNumber[] = new String[2];
        arrayWithStringAndNumber[0] = "-2.2052 dalam -2.7300";
        arrayWithStringAndNumber[1] = "-3.0511 dan akan -0.1116";

        String numArray1[] = new String[2];
        String numArray2[] = new String[2];
        String strArray[] = new String[2];

        String temp;
        for (int i = 0; i < arrayWithStringAndNumber.length; i++) {
            temp = arrayWithStringAndNumber[i];
            numArray1[i]=temp.substring(0,temp.indexOf(" "));
            numArray2[i]=temp.substring(temp.lastIndexOf(" ")+1);
            strArray[i]=temp.substring(temp.indexOf(" ")+1,temp.lastIndexOf(" "));
        }

Make sure all arrays are of same length. For num arrays use type whatever you want. I think you may need double and then you can easily parse the value to fit in it.

Hope this helps.


You can use indexOf(int ch) and lastIndexOf(int ch) of String object to find the first and last whitespace character and divide the string using these two indexes. You can also trim the middle string part if needed.

So:

String[] input; // given
Double[] firstNumbers = new Double[input.length];
String[] middleParts = new String[input.length];
Double[] secondNumbers = new Double[input.length];

for(int i = 0; i < input.length; i++) {
   String line = input[i];
   int firstWhitespace = line.indexOf(" ");
   int lastWhitespace = line.lastIndexOf(" ");
   String firstNumber = line.substring(0, firstWhitespace);
   String middlePart = line.substring(firstWhitespace, lastWhitespace+1);
   String secondNumber = line.substring(lastWhitespace+1, line.length());

   // parse numbers to double, add to an array
   firstNumbers[i] = Double.parseDouble(firstNumber);
   middleParts[i] = middlePart;
   secondNumbers[i] = Double.parseDouble(secondNumber);
}


Usually every programming language has functions for operating on strings data. Common set of functions is

  1. length (or len) - to get length of string
  2. find (or indexOf or somthing like this) - to find position of character of substring
  3. substring (or substr) - to get substring of N characters from postion P

often

  1. left/right - to get substring of N characters from left or right string's side
  2. Trim/leftTrim/rightTrim - to trim from left and/or right string's side all space-characters or given as function parameter character.

Always as you need to operate on strings data, try to read documentation or google. You always will find information at Internet. Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消