0

I need some help to extract a substring out of a string. Right now, my normal string is equal to:

"Einzelzimmer inkl Frühstück - 50,00↵Doppelzimmer inkl Frühstück - 65,00"

Code

var rooms = $('#quote-outbound-hotel-room').text();

if (rooms.indexOf(room) >= 0) {
    var roomsToQuote = "The new string created";

    $('#quote-outbound-hotel-room').text('');
    $('#quote-outbound-hotel-room').text(roomsToQuote);
}

rooms is eqaul to the big string I have showed. room is equal to one room, lets say:

"Einzelzimmer inkl Frühstück - 50,00"

How can I take out the substring room from the original string and return it to the quote?

The roomsToQuote should be the new string I will push to my quote.

Update:

When making an array and splitting it, the array only contains one element with the whole string like:

["Einzelzimmer inkl Frühstück - 50,00↵Doppelzimmer inkl Frühstück - 65,00"]

1 Answer 1

1

It's better if you split by line-breaks and you obtain an array that it's easy to manipulate.

var rooms = rooms.split("\r\n");
if($.inArray("roomXX", rooms) > -1) {
    // it's in array!  
}

And when you finish to merge the elements, join it

rooms.join("\r\n"); // you obtain the string
Sign up to request clarification or add additional context in comments.

7 Comments

if($.inArray("roomXX", rooms) > -1) { jQuery.inArray(), how to use it right?
I think the answer is right. So it's not a copy&paste answer, it's a clue to make the correct thing
so is this the best solution at the moment? What are you doing in the if statement? could you maybe explain?
It's basic programation. You have a string. You split it by line breaks. You obtain an array of elements. You search if your room exists in the array of rooms (inArray()), inside the if you can replace the elements. Finally you join the array and you obtain the final string with all elements.
I think the spilt command does not do it right. Have updated so you can see what it returns.
|

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.