I have a string like abc-5,xyz-9,pqr-15 Now,I want to get the value only after "-" So, how can i get that value..and i want this value in String Array?
You can try
String string = "abc-5,xyz-9,pqr-15";
String[] parts = string.split(",");
String val1 = parts[0].split("-");
.....
and so on
int pos = string.indexOf('-');
String sub = string.substring(pos);
If you have multiple values in each string, you'll have to split it first (using split method). For example:
String[] array = string.split(',');
String[] values = new String[array.length];
for(int i = 0; i < array.length; i++)
values[i] = array[i].substring(arrays[i].indexOf('-'));
Now you have the values in an array.
I would use split on your string.
String str = "abc-5,xyz-9,pqr-15";
String[] arr = str.split(",");
for (String elem: arr) {
System.out.print(elem.split("-")[1] + " : "); // Will print - `5 : 9 : 15`
}
or with Regular Expression like this: -
Matcher matcher = Pattern.compile("-(\\d+)").matcher(str);
while(matcher.find()) {
System.out.println(matcher.group(1));
}
Use:
String newString= oldString.substring(oldString.indexOf('-'));
Related
I have a string, which is a list of coordinates, as follows:
st = "((1,2),(2,3),(3,4),(4,5),(2,3))"
I want this to be converted to an array of coordinates,
a[0] = 1,2
a[1] = 2,3
a[2] = 3,4
....
and so on.
I can do it in Python, but I want to do it in Java.
So how can I split the string into array in java??
It can be done fairly easily with regex, capturing (\d+,\d+) and the looping over the matches
String st = "((1,2),(2,3),(3,4),(4,5),(2,3))";
Pattern p = Pattern.compile("\\((\\d+),(\\d+)\\)");
Matcher m = p.matcher(st);
List<String> matches = new ArrayList<>();
while (m.find()) {
matches.add(m.group(1) + "," + m.group(2));
}
System.out.println(matches);
If you genuinely need an array, this can be converted
String [] array = matches.toArray(new String[matches.size()]);
Alternative solution:
String str="((1,2),(2,3),(3,4),(4,5),(2,3))";
ArrayList<String> arry=new ArrayList<String>();
for (int x=0; x<=str.length()-1;x++)
{
if (str.charAt(x)!='(' && str.charAt(x)!=')' && str.charAt(x)!=',')
{
arry.add(str.substring(x, x+3));
x=x+2;
}
}
for (String valInArry: arry)
{
System.out.println(valInArry);
}
If you don't want to use Pattern-Matcher;
This should be it:
String st = "((1,2),(2,3),(3,4),(4,5),(2,3))";
String[] array = st.substring(2, st.length() - 2).split("\\),\\(");
How can I split a string by 2 characters with shifting.
For example;
My string is = todayiscold
My target is: "to","od","da","ay","yi","is","sc","co","ol","ld"
but with this code:
Arrays.toString("todayiscold".split("(?<=\\G.{2})")));
I get: `"to","da","yi","co","ld"
anybody helps?
Try this:
String e = "example";
for (int i = 0; i < e.length() - 1; i++) {
System.out.println(e.substring(i, i+2));
}
Use a loop:
String test = "abcdefgh";
List<String> list = new ArrayList<String>();
for(int i = 0; i < test.length() - 1; i++)
{
list.add(test.substring(i, i + 2));
}
Following regex based code should work:
String str = "todayiscold";
Pattern p = Pattern.compile("(?<=\\G..)");
Matcher m = p.matcher(str);
int start = 0;
List<String> matches = new ArrayList<String>();
while (m.find(start)) {
matches.add(str.substring(m.end()-2, m.end()));
start = m.end()-1;
}
System.out.println("Matches => " + matches);
Trick is to use end()-1 from last match in the find() method.
Output:
Matches => [to, od, da, ay, yi, is, sc, co, ol, ld]
You cant use split in this case because all split does is find place to split and brake your string in this place, so you cant make same character appear in two parts.
Instead you can use Pattern/Matcher mechanisms like
String test = "todayiscold";
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile("(?=(..))");
Matcher m = p.matcher(test);
while(m.find())
list.add(m.group(1));
or even better iterate over your Atring characters and create substrings like in D-Rock's answer
Actually this is a very simple question, I tried a lot but I am unable to get the exact solution. I have a string like:
String mystring = "one<1234567>,two<98765432>,three<878897656>";
Here I want the data which is inside "<" and ">". Can anyone help me with this?
I would use regex
String str = "one<1234567>,two<98765432>,three<878897656>";
Matcher m = Pattern.compile("<(.+?)>").matcher(str);
while(m.find()) {
String v = m.group(1);
}
Try
String mystring = "one<1234567>,two<98765432>,three<878897656>";
String[] result = mystring.split(",");
for (String s : result) {
s = s.substring(s.indexOf("<")+1);
s = s.substring(0, s.indexOf(">"));
System.out.println(s);
}
Print result :
1234567
98765432
878897656
You can use a regex like <(.*?)> :
String mystring = "one<1234567>,two<98765432>,three<878897656>";
Pattern pattern = Pattern.compile("<(.*?)>");
Matcher matcher = pattern.matcher(mystring);
while (matcher.find())
{
System.out.println(matcher.group(1));
}
Try this
String mystring = "one<1234567>,two<98765432>,three<878897656>";
String[] a = myString.split(",");
for(int i = 0; i < a.length; i++){
String substr=a[i].subString(a[i].indexOf("<"),a[i].indexOf(">"));
System.out.println(substr);
}
Try if your inner bracket value always numeric and outside alphabetical i.e. <, >
String[] strings=mystring.replaceAll("[a-z<>]", "").split(",");
for(String string:stringsArray)
{
System.out.println(string);
}
i found an new solution from StringTokenizer class
you can use it as,
StringTokenizer tokens = new StringTokenizer(KEY_SUBFOLDERNAME, ".");
String first_string = tokens.nextToken();
File_Ext = tokens.nextToken();
System.out.println("First_string : "+first_string);
System.out.println("File_Ext : "+File_Ext);
I want to split this string
['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12,345','01-02-2012', 'Test 2']
To an array of array string in java?
Can I do it with regex or should I write a recursive function to handle this purpose?
How about something like the following
String str= "['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12,345','01-02-2012', 'Test 2']";
String[] arr = str.split("\\],\\[");
String[][] arrOfArr = new String[arr.length][];
for (int i = 0; i < arr.length; i++) {
arrOfArr[i] = arr[i].replace("[", "").replace("]", "").split(",");
}
I'm not able to test this because of recent crash wiped out all my programs, but I believe you can use the JSON parsers to parse the string. You might have to wrap it in [ and ] or { and } before you parse.
See
http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/
http://www.json.org/java/
How to parse a JSON and turn its values into an Array?
You could use String.split and regex look-behind in combination:
String str = "['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12,345','01-02-2012', 'Test 2']";
String[] outerStrings = str.split("(?<=]),");
String[][] arrayOfArray = new String[outerStrings.length][];
for (int i=0; i < outerStrings.length; i++) {
String noBrackets = outerStrings[i].substring(1, outerStrings[i].length() - 1);
arrayOfArray[i] = noBrackets.split(",");
}
String yourString = "['1','BR_1142','12,345','01-02-2012', 'Test 1'],['2','BR_1142','12.345','01-02-2012', 'Test 2']";
yourString = yourString.substring(1, yourString.lastIndexOf("]"));
String[] arr = yourString.split("\\],\\[");
String[][] arr1 = new String[arr.length][];
int i = 0;
String regex = "(?<=['],)"; // This regex will do what you want..
for(String a : arr) {
arr1[i++] = a.split(regex);
}
for (String[] arrasd: arr1) {
for (String s: arrasd) {
System.out.println(s.replace(",", ""));
}
}
I have
String add_data[] = new String[6];
with datas at 0,1,2,3,4,5 indexes.
Also I have
ArrayList<String> data = new ArrayList<String>();
now I need to put the values in all the indexes in add_data at data[0].
How can i do this? please guide me.
Eg. add_data[0]="a";
add_data[0]="b";
data[0] should have "ab"
use Arrays.asList(array) method to copy array to List. In your case - String[] to List<String>.
String result = "";
for (int i = 0; i < add_data.length: i++) {
result += add_data[i];
}
data.add(result);
I'm not sure to understand what you want to do, but try this :
String temp = "";
for(String s : add_data) {
temp += s;
}
data.put(temp);
You need:
Catenate all Strings from your array
Put this at 0 index in your ArrayList
Something like this:
String addData[] = new String[6];
ArrayList<String> data = new ArrayList<String>();
addData[0] = "a";
addData[1] = "b";
// 1. catenate all strings
String str = "";
for (String s : addData) {
str += (s != null)?s:"";
}
// 2. put it into 0 index in your arraylist
data.add(0, str);
System.out.println(data.get(0));