Results 1 to 9 of 9
  1. #1
    slb056010 is offline Member
    Join Date
    Aug 2013
    Posts
    7
    Rep Power
    0

    Default how can i substring something

    class Searching {
    public static void main(String args[]){
    String str="sdad das > a > ssd i am a man .";

    int index1= str.indexOf('i');
    int index2=0;
    int count=0;

    for(int i=index1;i>0;i--){

    index2=str.indexOf('>',i);

    }

    System.out.print(str.substring(index2,index1));
    }
    }

    i need to substring '>' of near the letter i and this program output is > a > ssd but i just need > ssd how can i solve this problem and i need to substring chinese , is that using the getbtye?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: how can i substring something

    Java Code:
     str.substring(index2,index1)
    Why did you reverse index2 and index1?

    and i need to substring chinese , is that using the getbtye
    Certainly not, Chinese especially uses multibyte characters so a getByte() method is not going to help you at all. Just use the substring() method, once you've figure out how to use it correctly by checking the API documentation.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    slb056010 is offline Member
    Join Date
    Aug 2013
    Posts
    7
    Rep Power
    0

    Default Re: how can i substring something

    index2 is starting position i need to start the substring sdad das > a > ssd i am a man .
    --------------------------------------------------------------------------^--------^
    ---------------------------------------------------------------------------index2---index1

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: how can i substring something

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: how can i substring something

    Your code is really hard to understand. In stead of posting code and giving a vague description of what you want, what do you FUNCTIONALLY want to achieve. I'll give examples:

    1. I want everything from the start to the first '>' character
    2. I want everything between the two '>' characters
    3. I want everything from the last '>' character to the end of the String

    Once you can properly describe what you want to have, a technical solution will be easy.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: how can i substring something

    Quote Originally Posted by slb056010 View Post
    class Searching {
    public static void main(String args[]){
    String str="sdad das > a > ssd i am a man .";

    int index1= str.indexOf('i');
    int index2=0;
    int count=0;

    for(int i=index1;i>0;i--){

    index2=str.indexOf('>',i);

    }

    System.out.print(str.substring(index2,index1));
    }
    }

    i need to substring '>' of near the letter i and this program output is > a > ssd but i just need > ssd how can i solve this problem and i need to substring chinese , is that using the getbtye?
    no need to use getByte() .... by using lastIndexOf and indexOf methods are enough to do this problem..

    public class Searching {

    public static void main(String args[]){
    String str="sdad das > a > ssd i am a man .";

    int index1= str.indexOf('i');

    int x=str.lastIndexOf(">");
    System.out.println(x);
    int y=index1-x;
    y+=x;
    System.out.println(str.substring(x, y));
    }
    }

  7. #7
    rsalvador is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: how can i substring something

    You can try this code:

    Java Code:
    package com.teknoscope.java.tutorial.scanner;
    
    
    
    public class Test {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String str="sdad das > a > ssd i am a man .";
    		str = str.substring(0,str.indexOf("i")).substring(str.lastIndexOf(">")).trim();
    		System.out.println(str);
    		
    	}
    
    }



    As you would have noticed we have used several String class methods such as

    substring(beginIndex,lastIndex) - we have used it to chop off the string from 0, to the index of i

    indexOf(String str) - this method job is to find the index of which served as the lastIndex method parameter of substring method

    substring(beginIndex) - the result of two above methods were chopped again from the lastIndex of ">" until the length of the string

    trim() - just to remove leading and trailing white spaces
    Last edited by DarrylBurke; 08-30-2013 at 09:49 PM. Reason: Removed spammy links

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: how can i substring something

    Quote Originally Posted by rsalvador View Post
    You can try this code:
    I've removed the spammy links from your post. Any more like that and you will be banned.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    rsalvador is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: how can i substring something

    ok sure no problem. your site, your rules but my placement of links are correct and anchor text is fine, I did not place barebone links without an explanation thus it is not a spam.
    ~anyways thanks.

Similar Threads

  1. Getting substring ???
    By hacikho in forum New To Java
    Replies: 9
    Last Post: 11-29-2011, 05:17 AM
  2. substring Q
    By arsenal4ever_11 in forum NetBeans
    Replies: 1
    Last Post: 06-01-2010, 03:27 PM
  3. Help with substring
    By hiklior in forum New To Java
    Replies: 12
    Last Post: 06-28-2008, 06:48 AM
  4. How can i get substring
    By 82rathi.angara in forum New To Java
    Replies: 14
    Last Post: 06-27-2008, 04:35 AM
  5. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •