0

how to get string variables from a whole string in java.

if i am given a string which is created this way:

String s = "Name:" + name + "Age:" + age + "Sex:" + sex;

how can i get name, age, sex from this String s.

I cannot use getWord(position) or something like that because i dont know how many words will be there in name, and how the age is represented age = "22 years" or age = "22 years 5 months".

5
  • Does every "variable" start with a big letter? Commented May 9, 2012 at 7:55
  • 1
    If you don't have separators between your fields, this will be messy... Commented May 9, 2012 at 7:55
  • Your string concatenation doesn't have spaces, so, for example, I would come up as "Name:GregAge:32Sex:male" Commented May 9, 2012 at 7:57
  • 1
    No need to use regex. Simply getIndex('Age:') will get the end of the first value. Commented May 9, 2012 at 7:57
  • @ greg in my actual code i use spaces...but just to show to clearly i didnt show it here..sry Commented May 9, 2012 at 7:58

7 Answers 7

2

I suggest you to have the Name, Age and Sex splitted by "," character.

It means:

String s = "Name:" + name + ",Age:" + age + ",Sex:" + sex;

Then you can split the string s by ","

String properties = s.split(",");

Then from properties variable, you can split by ":" to take the Property name and the value of that property.

Is that clear?

I'm adding more code to support another ways as you wanted:

String name = "Thuan";
String age = "27";
String sex = "male";
String s = "Name:" + name + "Age:" + age + "Sex:" + sex;

int nameIndex = s.indexOf("Name:");
int ageIndex = s.indexOf("Age:");
int sexIndex = s.indexOf("Sex:");

String theName = s.substring(nameIndex + "Name:".length(), ageIndex);
String theAge = s.substring(ageIndex + "Age:".length(), sexIndex);
String theSex = s.substring(sexIndex + "Sex:".length(), s.length());

System.out.println(theName);
System.out.println(theAge);
System.out.println(theSex);

Please be note that this is just example show you the logic, you need to refactor yourself.

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

4 Comments

clear..thankyou... but is there a way i can achieve this without using delimiters...because i need to show the whole string as well...it wont look good with delimiters.
Yes. there is. Just define your property names as constants. Try to use them with s.startWiths and s.indexOf :) I guess it will works. But need to use the logic well.
For example: use s.indexOf("Name:") and you have the index of Name:, do the same with Age: and Sex:. You will have 3 indexes numbers, right? Then you sort them ascending. Now you can get it by index[0] -> index[1] => property 1, index[1] -> index[2] => property 2... so on...
And also please note that you should refactor the code yourself, my code is just example. For example: Name: and Age: and Sex: should be defined as Constants.
1

You will go on reading the name (after the "NAME:" ofcourse) until you are encountered with four subsequent letters "AGE:" and then go on reading the age until you are encountered with four subsequent letters "SEX:" and the rest is the sex data.

Edit: Actually " : " may be your seperator if it is not included in data.

2 Comments

According to the question, this might possibly be the correct way to do it, unless the variables have specific delimiting characters. And we assume that the words 'age' and 'sex' are not allowed to appear anywhere in the variable values.
@maress Yes, as I edited the post: If " : " is not included in the data for sure, it is easier to divide the string by the delimiting character " : ".
0

If you have a delimiting character you can use the String.split() method. For example, have

s = "Name: carmelo; Age: 25 years; Sex: m;"

then you can call

String[] values = s.split(";")
String name = values[0].split(":")[1];
String age = values[1].split(":")[1];
String sex = values[2].split(":")[1];

1 Comment

but is there a way i can achieve this without using delimiters...because i need to show the whole string as well...it wont look good with delimiters.
0

Make use of "Split"

String s = "Name:" + name + "#Age:" + age + "#Sex:" + sex;
String[] splits = s.split("#");

System.out.println("splits.size: " + splits.length);

for(int i=0; i < splits.length; i++){
//System.out.println(splits[i] );
String[] values= splits[i].split(":");

System.out.println(values[0]+" = " + values[1] );
}

Comments

0

There should be proper delimiters on the created String s, otherwise it would be impossible to split them into arrays of strings.

For example:

private static final String FIELD_DELIMITER = ";";
private static final String DATA_DELIMITER = "=";

String s = "Name" + DATA_DELIMITER + name + FIELD_DELIMITER +
           "Age" + DATA_DELIMITER + age + FIELD_DELIMITER +
           "Sex" + DATA_DELIMITER + sex + FIELD_DELIMITER;

Comments

0

you can use the code follow:

    String s = "Name:" + name + "Age:" + age + "Sex:" + sex;
    int i = s.indexOf("Age:");
    int j = s.indexOf("Sex:");
    String Name = s.substring(0,i).split(":")[1];
    String Age = s.substring(i,j).split(":")[1];
    String Sex = s.substring(j).split(":")[1];
    System.out.println("Name="+Name+" Age="+Age+" Sex="+Sex);

Comments

0

I suggest you to use regular expression. Sample code for you is :

String s = "NAME : " + " abhi " + " Age : " + " 22 " + " Sex : " + " m ";
Pattern pattern = Pattern.compile("NAME[\\s]*:[\\s]*(.*)[\\s]*Age[\\s]*:[\\s]*(.*)[\\s]*Sex[\\s]*:[\\s]*(.*)");
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
    System.out.println("NAME : " + matcher.group(1) + " Age : " + matcher.group(2) + " Sex : "
            + matcher.group(3));
}

Will work for you .You can do changes for you.

update:And this will work even though their are spaces or not between Name and .Doesnt matter on spaces and dont need to use any delimiter.

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.