1

I am trying to get an element from my string which I have attained through a getSelectedValue().toString from a JList. It returns [1] testString

What I am trying to do it only get the 1 from the string. Is there a way to only get that element from the string or remove all else from the string?

I have tried:

String longstring = Customer_list.getSelectedValue().toString();
int index = shortstring.indexOf(']');
String firstPart = myStr.substring(0, index); 
6
  • 1
    yes there is. Have you tried anything yet? Commented Oct 13, 2015 at 10:06
  • @KevinEsche I have tried String longstring; longstring = Customer_list.getSelectedValue().toString(); int index = shortstring.indexOf(']'); String firstPart = myStr.substring(0, index); Commented Oct 13, 2015 at 10:13
  • you allmost had it, String#substring(int) defines the substring from the beginning index to the end. This will just give you everything after the [1]. if you use String#substring(int,int), which is defined as create a substring from pos x to excluse pos y then you could do it like this: longstring.substring(0, longstring.indexOf("]") + 1). Commented Oct 13, 2015 at 10:16
  • This indeed did remove the second part of the string. Thank you! All i need to figure out is how to remove the "[" and "]". Would I do that through the same way? Commented Oct 13, 2015 at 10:19
  • you could make use of the String#replace(charSequence, charSequence) method. The first parameter should be either "[" or "]", your second could be a empty String as "". Just call it twice after each other and the brackets should be gone. Commented Oct 13, 2015 at 10:23

4 Answers 4

2

You have many ways to do it, for example

  1. Regex
  2. String#replaceAll
  3. String#substring

See below code to use all methods.

import java.util.*;
import java.lang.*;
import java.util.regex.Matcher;  
import java.util.regex.Pattern;

class Test {  
    public static void main(String args[]) {
    String[] data = { "[1] test", " [2] [3] text ", " just some text " };

    for (String s : data) {
        String r0 = null;
        Matcher matcher = Pattern.compile("\\[(.*?)\\]").matcher(s);
        if (matcher.find()) {
            r0 = matcher.group(1);
        }
        System.out.print(r0 + " ");
    }
    System.out.println();

    for (String s : data) {
        String r1 = null;
        r1 = s.replaceAll(".*\\[|\\].*", "");
        System.out.print(r1 + " ");
    }
    System.out.println();

    for (String s : data) {
        String r2 = null;
        int i = s.indexOf("[");
        int j = s.indexOf("]");
        if (i != -1 && j != -1) {
            r2 = s.substring(i + 1, j);
        }
        System.out.print(r2 + " ");
    }
    System.out.println();
    }
}

However results may vary, for example String#replaceAll will give you wrong results when input is not what you expecting.

1 2 null

1 3 just some text

1 2 null

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

Comments

0

What worked best for me is the String#replace(charSequence, charSequence) combined with String#substring(int,int)

I have done as followed:

        String longstring = Customer_list.getSelectedValue().toString();
        String shortstring = longstring.substring(0, longstring.indexOf("]") + 1);
        String shota = shortstring.replace("[", "");
        String shortb = shota.replace("]", "");

My string has been shortened, and the [ and ] have been removed thereafter in 2 steps.

Comments

0

If you know each input string begins with a number in square brackets followed by a SPACE character, then I would simply split the input by SPACE character, take the first part, and replace the square brackets with nothing. That leaves characters to be parsed as digits.

String input = "[1] testString";
String[] parts = input.split( " " );
String part = parts[0];
String digits = part.replace( "[" , "" ).replace( "]" , "" ) ;
int number = Integer.parseInt( digits ) ;

See this code run at Ideone.com.

1


Briefer:

String digits = input.split( " " )[0].replace( "[" , "" ).replace( "]" , "" ) ;
int number = Integer.parseInt( digits ) ;

Briefest:

int number = Integer.parseInt( input.split( " " )[0].replace( "[" , "" ).replace( "]" , "" ) ) ;

Comments

0

Yes, or more simply: getSelectedValue().replaceAll("\\[(.*?)\\]", "$1")

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.