TJF
TJF

Reputation: 45

Counting total Chars between two chars in a string - JAVA

Need help turning pseudo code into code.

The program would take in the input and print out if there is data occupied there. So if there is nothing after the ":", it would fail. In this example, there is nothing after the reason, so that test would fail, all others pass.

I figured the best way to go about this is to just check for data after the colon to see if there is more then one char(the blank space) between the colon and the total length of text before the next colon. So the check from Reason to Status would be

//This would be the check betweek the reason colon and status colon - 6 chars //becuase of the word status
if(lengthBetween_ReasoncolonAndStatusColon - 6 > 2){
   //PASS
}else{
   //fail
}

Format of Input

User Name: Chris Smith [email protected] Users Password: 123ABC Last Login Date: 2015/10/14 - 12:30AM Reason: Status: Online

Output

Username: PASS
Password: PASS
Last Login: PASS
REASON: FAIL
Status: PASS

Code

//This would be the check betweek the username and password - 14 chars //becuase of the words User Password
if(lengthBetween_UsernameColonAndPasswordColon - 14 > 2){
   //PASS, there must be data here
}else{
   //fail, no data
}

Any help would do. A good starting point would be to show me how to could the total chars between two "key" elements. So I could use some help with code to say there is X amount of chars between the first two colon pairs, X amount between the next pair, and so on.

Thanks!

Upvotes: 0

Views: 738

Answers (3)

Gobinath
Gobinath

Reputation: 904

I hope this is what you need:

String s = "User Name: Chris Smith [email protected] Users Password: 123ABC Last Login Date: 2015/10/14 - 12:30AM Reason: Status: Online";
int lengthBetweenReasonAndStatus = s.indexOf("Status:") - s.indexOf("Reason:");

if(lengthBetweenReasonAndStatus - 6 > 2) {
    System.out.println("Found");
}

Upvotes: 1

Jordi Castilla
Jordi Castilla

Reputation: 27001

String API has lot of built in functions you can use:

  • contains

    Returns true if and only if this string contains the specified sequence of char values.

  • indexOf

    Returns the index within this string of the first occurrence of the specified character. If a character with value ch occurs in the character sequence represented by this String object, then the index (in Unicode code units) of the first such occurrence is returned.

  • trim

    Returns a copy of the string, with leading and trailing whitespace omitted.

  • split

    Splits this string around matches of the given regular expression.

Play with them!!


If you feel you need a more powerful tool, please kindly check StringUtils from APACHE COMMONS.

Upvotes: 2

Abubakkar
Abubakkar

Reputation: 15664

You can use indexOf and lastIndexOf method from String class in java

String s = "rtabcedaf";
int count = s.lastIndexOf('a') - s.indexOf('a') - 1;  //returns 4

Hope that's enough for you to start with.

Upvotes: 1

Related Questions