Java: reading a string in a particular format - java

I am not posting any code I am struck with. I am trying this in Java:
Issue:
I have words like:
,xxxx-1223
yyyyy,xxdd-345
$,xxxxr-7
sdsdsdd-18
so what ever format I have I should be able to read the last one:
xxxx-1223
xxdd-345
xxxxr-7
sdsdsdd-18
what so may be the words, all I need to to get the words as shown.

Use String#lastIndexOf(int) to find where the last comma occurs, and use String#substring(int) to get the rest of the string that follows.
String input = /* whatever */;
int lastComma = input.lastIndexOf(',');
String output = input.substring(lastComma + 1);

String[] str=yourWord.split(",");
String output=str[str.length-1];

You can use this Regex: -
(\\w+-\\d+)$
Or this specific problem can simply be solved using String.split() or String.substring(int) methods

Related

Splitting string with similar starting pattern

So, I've been trying to split something I'm reading from a file. But everything that I've tried does not give me only the part that I want.
What I have as string is this:
Scenario:
Bunch of stuf here
Just typing stuff for the example...
Scenario:
More stuff here
A lot more stuff here
XX123
I want to get everything from 'Scenario:' to 'XX123'
Like this:
Scenario:
More stuff here
A lot more stuff here
XX123
The file that I'm reading from have a lot of those 'Scenarios:' and using Pattern from java doesn't give me only the part that I want. Instead it gives from the first 'Scenario:' it finds until 'XX123'
I also tried to use StringUtils.substringBetween, same result.
Thanks in advance
The old-fashioned way to do it would look something like this:
String inputText;
String END_MARKER = "XXX123";
int indexOfEnd = inputText.indexOf(END_MARKER);
// search in reverse
int indexOfScenario = inputText.lastIndexOf("Scenario", indexOfEnd);
String result = inputText.substring(indexOfScenario,
indexOfEnd + END_MARKER.length());

Printing an attribute inside quotes

I want to print an attribute of a class I am making , but I need it to be printed inside quotes " ".
I know it has something to with Escape Sequences but a similar post I found suggested using "\"Hello\"" for example to print "Hello"... My case is a bit more complicated cause I don't know beforehand the value of the attribute I want to print. So how can I do this?
Why don't make a function that make any String into a Quote. Exanple
public static String quotePrinter(String myQuote)
{
return "\"" +myQuote+ "\"";
}
String myQuote = "Hello World";
System.out.println(quotePrinter(myQuote));
And the output
"Hello World"
Not sure if i understand corretly want you want but take a look at the answers from #ataylor and #Martin Törnwall How to format strings in Java for String interpolation

how to extract unknown names out of a string

I'm trying to make a Minecraft Server control panel, and I want to get a list of all online players, each username in it's own String. The way you get the users is typing /list and it returns a string. the returned string looks like:
[HH:MM:SS] INFO: username1, username2, username3, and username4
so, how would i extract each username into it's own string? I've googled this, and looked as similar questions, and I cant find a useful answer. I thought about string.replaceAll(); but i cant seem to get that to work.
Any suggestions?
try using
String.split(", ");
This will split the string to an array.
Here is how> tutorial
String usernames = ...; // fill with data
String[] data = usernames.split(", ");
Afther this you must remove the date and time from the first name.
The previous answer with a few more technical details:
String[] usernames = String.split(",");
In order to extract the first username, you'll have to do something like:
String username1 = usernames[0].split("INFO:")[1]; // not sure if you need ":" or "\:", so check it out...
This is because usernames[0] == "[HH:MM:SS] INFO: username1", and you want to split it into the sub-string that appears before "INFO:" and the sub-string that appears after it.
In order to extract the remaining usernames, just iterate the usernames array from index 1.
For example:
for (int i=1; i<usernames.length; i++)
System.out.println(usernames[i]);
Note: you might want to strip off leading and/or trailing spaces, using strip().
Adding to what lucian said:
String [] data = s.split(", ");
data[0]=data[0].replaceAll("[HH:MM:SS] INFO: ", "");
Should replace the unwanted beginning of that string with nothing ("");

cannot parse String with Java Regex

I have a string formatted as below:
source1.type1.8371-(12345)->source2.type3.3281-(38270)->source4.type2.903..
It's a path, the number in () is the weight for the edge, I tried to split it using java Pattern as following:
[a-zA-Z.0-9]+-{1}({1}\\d+){1}
[a-zA-Z_]+.[a-zA-Z_]+.(\\d)+-(\\d+)
[a-zA-Z.0-9]+-{1}({1}\\d+){1}-{1}>{1}
hopefully it split the string into fields like
source1.type1.8371-(12345)
source2.type3.3281-(38270)
..
but none of them work, it always return the whole string as the field.
It looks like you just want String.split("->") (javadoc). This splits on the symbol -> and returns an array containing the parts between ->.
String str = "source1.type1.8371-(12345)->source2.type3.3281-(38270)->source4.type2.903..";
for(String s : str.split("->")){
System.out.println(s);
}
Output
source1.type1.8371-(12345)
source2.type3.3281-(38270)
source4.type2.903..
It seems to me like you want to split at the ->'s. So you could use something like str.split("->") If you were more specific about why you need this maybe we could understand why you were trying to use those complicated regexes

Add exact number of spaces to start of string using java.util.Formatter

I am using Formatter to output Java code to a file. I want to add a specific number of spaces to the start of each line. My problem is I cannot find a way to do this "neatly". The standard options seem to only allow adding a minimum number of spaces but not a specific number of spaces.
As a work around, I am currently doing the following:
out.format("%7s%s", "", "My text"); but I'd like to do it with only two arguments like this out.format("%7s", "My text");.
Does anyone know if there is a way to do this using the standard Formatter options?
I'm not exactly sure what you want here:
out.format("xxx%10sxxx", "My text");
// prints: xxx My textxxx
While:
out.format("xxx%-10sxxx", "My text");
// prints: xxxMy text xxx
As far as I know, there is no way to do the old C-style formatting to specify the size in an argument like "%*s" because then you could pass in (str.length() + 7).
I'm afraid that your way seems to the the most "neat". If you can explain why you don't like it maybe we can find a better workaround.
You can prepend text into your string.
Another way to reapet any string which you can use this code:-
String str = "abc";
String repeated = StringUtils.repeat(str, 3);
here StringUtils is org.apache.commons.lang3.StringUtils class.
Use Commons Lang
String line = "Hello World!";
int numberOfSpaces = 2;
String lineWithSpacePadding = StringUtils.leftPad(line, line.length() + numberOfSpaces);

Categories

Resources