0

I need to sub string the string after "2:" to the end of line as it is a changeable string:

Which mean in this example that I want to take the string "LOV_TYPE" from this 2 lines

ObjMgrSqlLog    Detail  4   2014-03-26 13:19:58 Bind variable 2: LOV_TYPE

ObjMgrSqlLog    Detail  4   2014-03-26 13:19:58 Bind variable 3: AUDIT_LEVEL

I tried to use subString(int startingIndex, int endingIndex) method, I can determine the first argument which is starting point.. but I can't determine the end point.

4 Answers 4

1

You can use two substrings, one that gets the String after 2:, and then one that gets the string before the next new line.

string = string.substring(string.indexOf("2:") + 2);
string = string.substring(0, string.indexOf("ObjMgrSqlLog));

If you need to get rid of the spaces on either end, you can then trim the string.

string = string.trim();
Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately these two lines are output of another program .. so i can't detect the new line by using "\n" !
@fanta You could get the index of the next ObjMgrSqlLog, then.
1

source:

String str = "ObjMgrSqlLog    Detail  4   2014-03-26 13:19:58 Bind variable 2: LOV_TYPE";

You can use regex

String out1 = str.replaceAll("^.*?.\\:.*[ ]", "");

or classic index-of

int lastCh = str.lastIndexOf(":");
String out2 = str.substring(++lastCh).trim();

output:

System.out.println(out1);
System.out.println(out2);

4 Comments

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ ) Have you actually tried this code?
fixed! and I added other option.
now you forgot the 2nd param of replaceAll method ;-)
the first one is very simple, just the source and the pattern, but it is slow, the other one is faster.
0

If you use str.substring(startingIndex), you will have the substring to the end of the string. It seems to be what you want. If you have extra spaces at the end of the string, you can always use a str.trim() to remove the spaces.

2 Comments

i need to sub string the word "LOV_TYPE" only .. not the whole sentence after "2:" .. and there is no spaces after this word "LOV_TYPE" .. only new line and new sentence
@fanta if you have "\n" at the end of the string, you can substring from startingPosition to str.length() - 1. Then you will not have the last character
0

Use substring along with .length() to get the value of the length of the string. For example:

    String original = "ObjMgrSqlLog    Detail  4   2014-03-26 13:19:58 Bind variable 2: LOV_TYPE";
    String newString = original.substring (62, original.length ());
    System.out.print (newString);

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.