Java - find indexof for part of a needle in array haystack - java

I am trying to find what array index position contains part of a string.
FullDirPrt = "a1a" "b2b" "c3c"
String doT ="b2";
int DotPos = Arrays.asList(FullDirPrt).indexOf(doT);
If I search for b2b it returns indexOf. If I search for only b2, it returns -1.

You have to check each item in the array individually, as each array item is a separate string:
String[] full = { "a1a", "b2b", "c3c" };
String doT = "b2";
for(int i = 0; i < full.length; i++) {
if(full[i].contains(doT)) {
System.out.println("Found item at index " + i);
}
}

You are matching on the entire string, you are going to have to loop through the whole list or array, and then check each indexOf on each of those strings.
for (String s : FullDirPrt) {
if (s.indexOf("bs") > 0) {
// do something
}
}

With Simple String array without using List also you can do like below.
String[] full={"a1a", "b2b", "c3c"};
String doT = "b2";
for(String str:full){
if(str.contains(doT)){
System.out.println(str+" contains "+doT);
}
}

Related

How to find the last word in a string

I'm trying to create a method that returns the last word in a string but I am having some trouble writing it.
I am trying to do it by finding the last blank space in the string and using a substring to find the word. This is what I have so far:
String strSpace=" ";
int Temp; //the index of the last space
for(int i=str.length()-1; i>0; i--){
if(strSpace.indexOf(str.charAt(i))>=0){
//some code in between that I not sure how to write
}
}
}
I am just beginning in Java so I don't know many of the complicated parts of the language. It would be much appreciated if someone could help me find a simple way to solve this problem. Thanks!
You can do this:
String[] words = originalStr.split(" "); // uses an array
String lastWord = words[words.length - 1];
and you've got your last word.
You are splitting the original string at every space and storing the substrings in an array using the String#split method.
Once you have the array, you are retrieving the last element by taking the value at the last array index (found by taking array length and subtracting 1, since array indices begin at 0).
String str = "Code Wines";
String lastWord = str.substring(str.lastIndexOf(" ")+1);
System.out.print(lastWord);
Output:
Wines
String#lastIndexOf and String#substring are your friends here.
chars in Java can be directly converted to ints, which we'll use to find the last space. Then we'll simply substring from there.
String phrase = "The last word of this sentence is stackoverflow";
System.out.println(phrase.substring(phrase.lastIndexOf(' ')));
This prints the space character itself too. To get rid of that, we just increment the index at which we substring by one.
String phrase = "The last word of this sentence is stackoverflow";
System.out.println(phrase.substring(1 + phrase.lastIndexOf(' ')));
If you don't want to use String#lastIndexOf, you can loop through the string and substring it at every space until you don't have any left.
String phrase = "The last word of this sentence is stackoverflow";
String subPhrase = phrase;
while(true) {
String temp = subPhrase.substring(1 + subPhrase.indexOf(" "));
if(temp.equals(subPhrase)) {
break;
} else {
subPhrase = temp;
}
}
System.out.println(subPhrase);
You can use: (if you are not familiar with arrays or unusual methods)
public static String lastWord(String a) // only use static if it's in the
main class
{
String lastWord = "";
// below is a new String which is the String without spaces at the ends
String x = a.trim();
for (int i=0; i< x.length(); i++)
{
if (x.charAt(i)==' ')
lastWord = x.substring(i);
}
return lastWord;
}
you just need to traverse the input string from tail when first find blank char stop traverse work and return the word.a simple code like this:
public static String lastWord(String inputs) {
boolean beforWords = false;
StringBuilder sb = new StringBuilder();
for (int i = inputs.length() - 1; i >= 0; i--) {
if (inputs.charAt(i) != ' ') {
sb.append(inputs.charAt(i));
beforWords = true;
} else if (beforWords){
break;
}
}
return sb.reverse().toString();
}
You could try:
System.out.println("Last word of the sentence is : " + string.substring (string.lastIndexOf (' '), string.length()));

searching a Char letter by letter

Trying to search for patterns of letters in a file, the pattern is entered by a user and comes out as a String, so far I've got it to find the first letter by unsure how to make it test to see if the next letter also matches the pattern.
This is the loop I currently have. any help would be appreciated
public void exactSearch(){
if (pattern==null){UI.println("No pattern");return;}
UI.println("===================\nExact searching for "+patternString);
int j = 0 ;
for(int i=0; i<data.size(); i++){
if(patternString.charAt(i) == data.get(i) )
j++;
UI.println( "found at " + j) ;
}
}
You need to iterate over the first string until you find the first character of the other string. From there, you can create an inner loop and iterate on both simultaneously, like you did.
Hint: be sure to look watch for boundaries as the strings might not be of the same size.
You can try this :-
String a1 = "foo-bar-baz-bar-";
String pattern = "bar";
int foundIndex = 0;
while(foundIndex != -1) {
foundIndex = a1.indexOf(pattern,foundIndex);
if(foundIndex != -1)
{
System.out.println(foundIndex);
foundIndex += 1;
}
}
indexOf - first parameter is the pattern string,
second parameter is starting index from where we have to search.
If pattern is found, it will return the starting index from where the pattern matched.
If pattern is not found, indexOf will return -1.
String data = "foo-bar-baz-bar-";
String pattern = "bar";
int foundIndex = data.indexOf(pattern);
while (foundIndex > -1) {
System.out.println("Match found at: " + foundIndex);
foundIndex = data.indexOf(pattern, foundIndex + pattern.length());
}
Based on your request, you can use this algorithm to search for your positions:
1) We check if we reach at the end of the string, to avoid the invalidIndex error, we verify if the remaining substring's size is smaller than the pattern's length.
2) We calculate the substring at each iteration and we verify the string with the pattern.
List<Integer> positionList = new LinkedList<>();
String inputString = "AAACABCCCABC";
String pattern = "ABC";
for (int i = 0 ; i < inputString.length(); i++) {
if (inputString.length() - i < pattern.length()){
break;
}
String currentSubString = inputString.substring(i, i + pattern.length());
if (currentSubString.equals(pattern)){
positionList.add(i);
}
}
for (Integer pos : positionList) {
System.out.println(pos); // Positions : 4 and 9
}
EDIT :
Maybe it can be optimized, not to use a Collection for this simple task, but I used a LinkedList to write a quicker approach.

Find secuentially occurrences of all String[] in a given String

I have a pair of Strings in an array to check in another String:
String[] validPair = "{"[BOLD]", "[/BOLD]" };
String toCheck = "Example [BOLD]bold long text[/BOLD] other example [BOLD]bold short[/BOLD]";
I need to check the balance of the tags, I know how to check if a string is inside another string, also how to achieve this using both indexOf of validPair content across the string and saving references, but is an ugly way and I don't wanna reinvent the wheel.
Something like :
int lastIndex = 0;
while (lastIndex != -1) {
int index = toCheck.findNextOccurrence(validPair, lastIndex); // here use indexOf
System.out.println(index);
lastIndex = index;
}
I was guessing if there is a way I can check nextOccurrence of any of the String's in String[] validPair in the String toCheck?
A kind of Iterator or Tokenizer but not splitting the string and giving only occurrences of the contents of the array (or List or any other Object).
OR:
OwnIterator ownIterator = new OwnIterator<String>(toCheck, validPair);
while (toCheck.hasNext()) {
String next = toCheck.findNextOccurrence();
System.out.println(next);
}
OUTPUT:
[BOLD]
[/BOLD]
[BOLD]
[/BOLD]
This is the solution I came up with. it is using array of regular expressions to search for every item in validPair separetely then combine all found occurrences into one list (and its iterator)
public class OwnIterator implements Iterator
{
private Iterator<Integer> occurrencesItr;
public OwnIterator(String toCheck, String[] validPair) {
// build regex to search for every item in validPair
Matcher[] matchValidPair = new Matcher[validPair.length];
for (int i = 0 ; i < validPair.length ; i++) {
String regex =
"(" + // start capturing group
"\\Q" + // quote entire input string so it is not interpreted as regex
validPair[i] + // this is what we are looking for, duhh
"\\E" + // end quote
")" ; // end capturing group
Pattern p = Pattern.compile(regex);
matchValidPair[i] = p.matcher(toCheck);
}
// do the search, saving found occurrences in list
List<Integer> occurrences = new ArrayList<>();
for (int i = 0 ; i < matchValidPair.length ; i++) {
while (matchValidPair[i].find()) {
occurrences.add(matchValidPair[i].start(0)+1); // +1 if you want index to start at 1
}
}
// sort the list
Collections.sort(occurrences);
occurrencesItr = occurrences.iterator();
}
#Override
public boolean hasNext()
{
return occurrencesItr.hasNext();
}
#Override
public Object next()
{
return occurrencesItr.next();
}
}
a quick test :
public static void main(String[] args)
{
String[] validPair = {"[BOLD]", "[/BOLD]" };
String toCheck = "Example [BOLD]bold long text[/BOLD] other example [BOLD]bold short[/BOLD]";
OwnIterator itr = new OwnIterator(toCheck, validPair);
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
gives desired output:
9
29
51
67
EDIT:
found a better solution, with just one regular expression that includes all items in validPair with "or" condition (|). then you have the Matcher's own find() method as the iterator:
String regex = "(";
for (int i = 0 ; i < validPair.length ; i++) {
regex += (i == 0 ? "" : "|") + // add "or" after first item
"\\Q" + // quote entire input string so it is not interpreted as regex
validPair[i] + // this is what we are looking for, duhh
"\\E"; // end quote
}
regex += ")";
System.out.println("using regex : " + regex);
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(toCheck);
while (m.find()) {
System.out.println(m.group(0));
}
you get the output
using regex : (\Q[BOLD]\E|\Q[/BOLD]\E)
[BOLD]
[/BOLD]
[BOLD]
[/BOLD]
You can just do:
int first = toCheck.indexOf(validPair[0]);
boolean ok = first > -1 && toCheck.indexOf(validPair[1], first) > 0;

Splitting up an equation using an ArrayList

If I enter an equation like:
9+3*2/1
My output is:
[9,3,2,1]
[,+,*,/]
Why does my second array start off with a "," and how do I get rid of it so the output would be
[+,*,/]
String evaluate(String exp) {
String setExpression = expr.getText();
String[] numbers = setExpression.split("[*/+-]");
String[] ops = setExpression.split("[123456789]");
ArrayList <String> setNumbers = new ArrayList <String>();
ArrayList <String> setOps = new ArrayList <String>();
for(int i=0; i<numbers.length; i++){
setNumbers.add(numbers[i]);
}
for(int i=0; i<numbers.length; i++){
setOps.add(ops[i]);
}
System.out.println(setNumbers);
System.out.println(setOps);
return exp;
}
}
An example similar to yours:
public class DemoSplit {
public static void main(String[] args) {
String s = ",1,2,";
String su[] = s.split(",");
for (String st: su) System.out.println("'"+st+"'");
}
}
Will print:
''
'1'
'2'
The first char is a delimiter, so split there and the element before it is the first element of th esplitted array.
In your case you spliut on numbers:
9+3*2/1
before 9, empty element, after '+' ...
If you feel like looking at the code, String.split calls java.util.regex.Pattern.split
In the split method, you will see that adds to an ArrayList the subStrings between index and the match, since index is zero for the first iteration and the first match in the example is at zero, the first element is an empty string. From the source code of the split method:
ArrayList<String> matchList = new ArrayList<String>();
Matcher m = matcher(input);
// Add segments before each match found
while(m.find()) {
if (!matchLimited || matchList.size() < limit - 1) {
String match = input.subSequence(index, m.start()).toString();
matchList.add(match);
index = m.end();
} else if (matchList.size() == limit - 1) { // last one
String match = input.subSequence(index,
input.length()).toString();
matchList.add(match);
index = m.end();
}
}
The extra empty string is what comes before 9 in your split. You're telling it to split on digits - there'll clearly be an extra empty string at the beginning and at the end.
The only reason why you don't see an extra empty string at the end is that when you copy the split to the ArrayList, you stop at numbers.length.
You will also have extra empty strings in here if you have numbers with more than one digit.
You can change the start of the second for loop so i = 1. The first element in ops is an empty string because of the way you split it.

Every combination of character array

Having problems trying to show every combination of a character of array without repeating letters.
public static String[] getAllLists(String[] elements, int lengthOfList)
{
//initialize our returned list with the number of elements calculated above
String[] allLists = new String[(int)Math.pow(elements.length, lengthOfList)];
//lists of length 1 are just the original elements
if(lengthOfList == 1) return elements;
else
{
//the recursion--get all lists of length 3, length 2, all the way up to 1
String[] allSublists = getAllLists(elements, lengthOfList - 1);
//append the sublists to each element
int arrayIndex = 0;
for(int i = 0; i < elements.length; i++)
{
for(int j = 0; j < allSublists.length; j++)
{
//add the newly appended combination to the list
allLists[arrayIndex] = elements[i] + allSublists[j];
arrayIndex++;
}
}
return allLists;
}
}
The above code works perfect but use's each letter more than once which cant be done in this case.
And i am stuck how to do this now.
Here's an example implementation. Essentially it takes a String and iterates over every character, putting that character at the front. It then recurses on the remaining characters. That structure removes your issue of repeated letters, because the input to the recursion has removed the character you've already used.
I also stored results in a set to remove semantic equivalences. The input 'aab' can switch char 0 and char 1 but still be 'aab.' I used a TreeSet to preserve ordering for easier verification of the output, but HashSet would be faster.
public static Set<String> permute(String chars)
{
// Use sets to eliminate semantic duplicates (aab is still aab even if you switch the two 'a's)
// Switch to HashSet for better performance
Set<String> set = new TreeSet<String>();
// Termination condition: only 1 permutation for a string of length 1
if (chars.length() == 1)
{
set.add(chars);
}
else
{
// Give each character a chance to be the first in the permuted string
for (int i=0; i<chars.length(); i++)
{
// Remove the character at index i from the string
String pre = chars.substring(0, i);
String post = chars.substring(i+1);
String remaining = pre+post;
// Recurse to find all the permutations of the remaining chars
for (String permutation : permute(remaining))
{
// Concatenate the first character with the permutations of the remaining chars
set.add(chars.charAt(i) + permutation);
}
}
}
return set;
}
Example run:
public static void main(String[] args)
{
for (String s : CharPermuter.permute("abca"))
{
System.out.println(s);
}
}
Generates:
aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaa

Categories