0

I need to remove a ';' from a string array. Any solutions?

What I did in my class was retrieving a long piece of string which are connected without spaces

(thisisalongpieceofstringtobeseparated)

I separated them by inserting a ';' between them

(this;is;a;long;piece;of;string;to;be;separated;)

Then I used .split(";") to add them to an array. All is successful but the last index which has a ';'.

this 

is 

a 

long 

piece 

of 

string 

to 

be 

separated; 


Need to remove the last ;

As requested, code is below.

 while (line != null) {

            if(line.contains("../../"))
            {

                int startIndex = line.indexOf("../../") + 6;
                int endIndex = line.indexOf(">", startIndex + 1);
                String abc = "http://www.google.com/";
                String imageSrc = line.substring(startIndex,endIndex);

                //complete full url
                String xyz = abc +imageSrc;
                xyz = xyz.substring(0,xyz.indexOf('"'));
                xyz = xyz +";";
                content += xyz;
                            mStrings = content.split(";");
4
  • I would be easier to generate a correctly serialized string in the first place. eg: (a;b;c) instead of (a;b;c;) Commented Dec 9, 2011 at 6:25
  • Can you show us the code please ? I can't reproduce your problem using String.split Commented Dec 9, 2011 at 6:27
  • @Hend: post your code. you have problem somewhere else in the code. Commented Dec 9, 2011 at 6:29
  • Alright I have added in the codes. PLease help me with it. Thank you so much Commented Dec 9, 2011 at 7:09

4 Answers 4

2
    String str="This; is; my; test;";
    String[] strArr=str.split(";");
    for(int i=0;i<strArr.length;i++)
    {
        System.out.println("Str:::"+strArr[i]);
     // add strArr[i] to an array or go through Arrays.toString()
    }

Nothing special in the code, i mean if you are using spilt() then it's already worked for you, if not then there is something wrong with your code,Post your code to see what exactly wrong with your code or else do this way

Sign up to request clarification or add additional context in comments.

Comments

2

I don't think you're telling us the whole story, because a trailing ; will be ignored when you split on ;. See for yourself by running this test code:

public static void main(String[] args) {
    System.out.println(Arrays.toString("a;b;c;".split(";")));
}

Output:

[a, b, c]

The "answer" is to do nothing! It should already work!

Comments

1

Wouldn't it be easiest just to remove the final ; from the string before splitting it ? That is, remove the last character (presuming that is always going to be ;) :

var newStr = str.substring(0, str.length-1);

And then split on that string instead.

Comments

1

Mikel's solution is a lot less work, but you could also use a StringTokenizer and specify returnDelims as false.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.