How to get a substring after four spaces in a given line?

I have a line like "align is going to school sad may me". I want to get a substring after four spaces. A string will be entered at run time. can someone suggest me find a Sub String after some set of spaces ......

String st = "align is going to school sad may me";

int i = 0;
String [] strings = new String [15];
StringTokenizer stringTokenizer = new StringTokenizer (st, " ");

while (stringTokenizer.hasMoreElements ())
{
    strings [i]= (String)stringTokenizer.nextElement ();
    i++;
}
System.out.println ("I value is" + i);

for (int j=4; j<i; j++)
{
    System.out.print (strings[j] + " ");
}

I tried this one and it works, can you suggest me an easy way to find the Sub string after some set of spaces.

+5
source share
6 answers
st = st.replaceAll("^(\\S*\\s){4}", "");

^ indicates that we remove only the first character of the string.

\s- any empty space. It will also delete, for example, tables.

\s - any character without a space.

* means any number of occurrences of a character.

So, \S*- any number of characters of a non-white space before a space.

{4}, , , 4 .

:

st = st.replaceFirst("(\\S*\\s){4}", "");

, ^.

4 :

st = st.replaceAll("^(\\S*\\s){1,4}", "");

, . , trim:

st = st.trim().replaceAll("^(\\S*\\s){1,4}", "");
+10

split?

st.split (" ", 5) [4]

5 . ( 4) .

, 4 , :

String [] chunks = st.split (" ", 5);
String tail = chunks.length == 5 ? chunks [4] : null;

null, .

+5
public static void main(String[] args) {
    String st = "   align is going to school sad may me   ";
    String trim = st.trim(); // if given string have space before and after string.
    String[] splitted = trim.split("\\s+");// split the string into words.
    String substring = "";
    if (splitted.length >= 4) { // checks the condition
        for (int i = 4; i < splitted.length; i++)
            substring = substring + splitted[i] + " ";
    }
    System.out.println(substring);

}
+1
public class MySplit {
    public static void main(String agsp[]) {
        String oldString = "roma h totti milan kaka juve love";
        String[] allStrings = oldString.split("\\s");
        String newString = "";
        for (int i = 3; i < allStrings.length; i++)
            newString = newString + " " + allStrings[i];
        System.out.println(newString);
    }
}

​​

public String newSplit(String data, int index){
            String[] allStrings = data.split("\\s");
            String newString = "";
            for (int i = index; i < allStrings.length; i++)
                newString = newString + " " + allStrings[i];
            return newString
}
0

, (just str.indexOf(' ')). :

String str ="ada adasd dasdsa d adasdad dasasd";

int targetMatch = 4;
int offset = 0;
for(int i = 0 ; i < targetMatch; i++){
     int position = str.indexOf(' ', offset);
     if(position != -1){
          System.out.println("position: "+ position);
          offset = position+1;
        }
     }

String result = str.substring(offset);
System.out.println(result);

For a real project ... extended regex would be better.

0
source

Here is a trivial and simple implementation that solves your problem:

String s = "I've tried this one and it working can you please suggest";

int index = -1;
for (int i = 0; i < 4; i++) {
    index = s.indexOf(' ', index + 1);
}
System.out.println(s.substring(index + 1));

This will fail if the line starts with a space or if it contains sequences of spaces. But this is the beginning.

Output: and it working can you please suggest

0
source

All Articles