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"]
Mandersen
  • 805
  • 3
  • 14
  • 22

1 Answers1

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
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • 1
    `if($.inArray("roomXX", rooms) > -1) {` [jQuery.inArray(), how to use it right?](http://stackoverflow.com/questions/18867599/jquery-inarray-how-to-use-it-right) – Tushar Dec 11 '15 at 11:37
  • I think the answer is right. So it's not a copy&paste answer, it's a clue to make the correct thing – Marcos Pérez Gude Dec 11 '15 at 11:40
  • so is this the best solution at the moment? What are you doing in the if statement? could you maybe explain? – Mandersen Dec 11 '15 at 11:42
  • 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. – Marcos Pérez Gude Dec 11 '15 at 11:50
  • I think the spilt command does not do it right. Have updated so you can see what it returns. – Mandersen Dec 11 '15 at 11:57
  • You've obtain that symbol `↵` exactly? So test the split with it. `rooms.split("↵")` – Marcos Pérez Gude Dec 11 '15 at 12:00
  • I used \n in the split method, but used the javascript splice method to extract the room from the string. – Mandersen Dec 11 '15 at 12:36